Skip to content

Commit

Permalink
Fix parsing heuristics for sizing bitmaps and enums (#30003)
Browse files Browse the repository at this point in the history
* Fix heuristics and add unit test

* Link the issue in DM for the sizing heuristic

* More unit test

* Support bitmap64, remove enum32, add more tests

* Unit test for bitmap8
  • Loading branch information
andy31415 authored and pull[bot] committed Dec 13, 2023
1 parent de66e56 commit 1216952
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,24 @@ def EndProcessing(self):

# try to find the best size that fits
# TODO: this is a pure heuristic. XML containing this would be better.
acceptable = {8, 16, 32}
# https://github.com/csa-data-model/projects/issues/345
acceptable = {8, 16, 32, 64}
for entry in self._bitmap.entries:
if entry.code > 0xFF:
if entry.code > 0xFF and 8 in acceptable:
acceptable.remove(8)
if entry.code > 0xFFFF:
if entry.code > 0xFFFF and 16 in acceptable:
acceptable.remove(16)
if entry.code > 0xFFFFFFFF and 32 in acceptable:
acceptable.remove(32)

if 8 in acceptable:
self._bitmap.base_type = "bitmap8"
elif 16 in acceptable:
self._bitmap.base_type = "bitmap16"
else:
elif 32 in acceptable:
self._bitmap.base_type = "bitmap32"
else:
self._bitmap.base_type = "bitmap64"

self._cluster.bitmaps.append(self._bitmap)

Expand Down Expand Up @@ -221,19 +226,16 @@ def EndProcessing(self):

# try to find the best enum size that fits out of enum8, enum32 and enum32
# TODO: this is a pure heuristic. XML containing this would be better.
acceptable = {8, 16, 32}
# https://github.com/csa-data-model/projects/issues/345
acceptable = {8, 16}
for entry in self._enum.entries:
if entry.code > 0xFF:
if entry.code > 0xFF and 8 in acceptable:
acceptable.remove(8)
if entry.code > 0xFFFF:
acceptable.remove(16)

if 8 in acceptable:
self._enum.base_type = "enum8"
elif 16 in acceptable:
self._enum.base_type = "enum16"
else:
self._enum.base_type = "enum32"
self._enum.base_type = "enum16"

self._cluster.enums.append(self._enum)

Expand Down
75 changes: 75 additions & 0 deletions scripts/py_matter_idl/matter_idl/test_data_model_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,81 @@ def testBasicInput(self):

self.assertEqual(xml_idl, expected_idl)

def testEnumRange(self):
# Check heuristic for enum ranges

xml_idl = XmlToIdl('''
<cluster id="123" name="Test" revision="1">
<dataTypes>
<bitmap name="Basic">
<bitfield name="One" bit="0">
<mandatoryConform/>
</bitfield>
<bitfield name="Two" bit="1">
<mandatoryConform/>
</bitfield>
<bitfield name="Three" bit="2">
<mandatoryConform/>
</bitfield>
</bitmap>
<bitmap name="OneLarge">
<bitfield name="Ten" bit="10">
<mandatoryConform/>
</bitfield>
</bitmap>
<bitmap name="LargeBitmap">
<bitfield name="One" bit="0">
<mandatoryConform/>
</bitfield>
<bitfield name="Ten" bit="10">
<mandatoryConform/>
</bitfield>
<bitfield name="Twenty" bit="20">
<mandatoryConform/>
</bitfield>
</bitmap>
<bitmap name="HugeBitmap">
<bitfield name="Forty" bit="40">
<mandatoryConform/>
</bitfield>
</bitmap>
</dataTypes>
</cluster>
''')

expected_idl = IdlTextToIdl('''
client cluster Test = 123 {
bitmap Basic: bitmap8 {
kOne = 0x01;
kTwo = 0x02;
kThree = 0x04;
}
bitmap OneLarge: bitmap16 {
kTen = 0x400;
}
bitmap LargeBitmap: bitmap32 {
kOne = 0x1;
kTen = 0x400;
kTwenty = 0x100000;
}
bitmap HugeBitmap: bitmap64 {
kForty = 0x10000000000;
}
readonly attribute attrib_id attributeList[] = 65531;
readonly attribute event_id eventList[] = 65530;
readonly attribute command_id acceptedCommandList[] = 65529;
readonly attribute command_id generatedCommandList[] = 65528;
readonly attribute bitmap32 featureMap = 65532;
readonly attribute int16u clusterRevision = 65533;
}
''')

self.assertEqual(xml_idl, expected_idl)

def testAttributes(self):
# Validate an attribute with a type list
# This is a very stripped down version from the original AudioOutput.xml
Expand Down

0 comments on commit 1216952

Please sign in to comment.