forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a codegen that converts idl back into .matter formats (project…
…-chip#29867) * Start creating a IDL codegen so we can self-test parsed output * Start with listing clusters * Enum listing * A lot more things supported * Attribute rendering * Support for string and octet string sizes * Timed command support * Restyle * Add descriptions to clusters * Attempt to fix up alignment of things * Alignment looks slightly better * Better command separation * Align comments * Align and output descriptions including clusters * More work regarding loop structures * Apply hex formatting to bitmaps. output now seems identical except one whitespace change * Identical output for now * Support API maturity. Notice that doccomments are lost on maturity :( * Fix doxygen parsing for api maturity at the cluster level * Restyle * Support endpoints, although that is not 1:1 as hex encoding and ordering for events is lost * Restyle * Add todo note that default value does not string escaping * Default rendering and add to files * More updates on file dependencies * Unit test IDL generator * Add the IDL unit test as a standard unit test * Update for python compatibility * Fix unit testing of builds when GSDK root is defined * Added a readme file * Restyle * Make xml parser use the idl codegen * Restyle * look to fix misspell warnings * Undo repo update * Fix linter errors --------- Co-authored-by: Andrei Litvin <[email protected]>
- Loading branch information
1 parent
9693c71
commit 422cc39
Showing
12 changed files
with
536 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
scripts/py_matter_idl/matter_idl/generators/idl/MatterIdl.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
{% macro render_field(field) -%}{# | ||
Macro for the output of a single field entry such as: | ||
int16u identifyTime = 0; | ||
optional int16u transitionTime = 3; | ||
optional nullable int16u transitionTime = 2; | ||
optional ExtensionFieldSet extensionFieldSets[] = 5; | ||
#} | ||
|
||
{%- if field.qualities %}{{field.qualities | idltxt}} {% endif -%} | ||
{{field.data_type.name}} | ||
{%- if field.data_type.max_length -%} <{{field.data_type.max_length}}> {%- endif -%} | ||
{##} {{field.name}} | ||
{%- if field.is_list %}[]{% endif -%} | ||
{##} = {{field.code}}; | ||
{%- endmacro -%} | ||
|
||
{% macro render_struct(s) -%}{# | ||
Macro for the output of a complete struct | ||
#} | ||
{%- if s.tag %}{{s.tag | idltxt}} {% endif -%} | ||
{% if s.qualities %}{{s.qualities | idltxt}} {% endif -%} | ||
struct {{s.name}} {##} | ||
{%- if s.code is not none %}= {{s.code}} {% endif -%} | ||
{ | ||
{% for field in s.fields %} | ||
{{render_field(field)}} | ||
{% endfor %} | ||
} | ||
{%- endmacro -%} | ||
|
||
|
||
// This IDL was auto-generated from a parsed data structure | ||
|
||
{% for cluster in idl.clusters %} | ||
{% if cluster.description %}/** {{cluster.description}} */ | ||
{% endif %} | ||
{{cluster.api_maturity | idltxt}}{{cluster.side | idltxt}} cluster {{cluster.name}} = {{cluster.code}} { | ||
{%- for enum in cluster.enums %} | ||
|
||
enum {{enum.name}} : {{ enum.base_type}} { | ||
{% for entry in enum.entries %} | ||
{{entry.name}} = {{entry.code}}; | ||
{% endfor %} | ||
} | ||
{% endfor %} | ||
|
||
{%- for bitmap in cluster.bitmaps %} | ||
|
||
bitmap {{bitmap.name}} : {{ bitmap.base_type}} { | ||
{% for entry in bitmap.entries %} | ||
{{entry.name}} = 0x{{"%X" | format(entry.code)}}; | ||
{% endfor %} | ||
} | ||
{% endfor %} | ||
|
||
{%- for s in cluster.structs | rejectattr("tag") %} | ||
{% if loop.first %} | ||
|
||
{% endif %} | ||
{{render_struct(s)}} | ||
{% if not loop.last %} | ||
|
||
{% endif %} | ||
{% endfor %} | ||
|
||
{%- for e in cluster.events %} | ||
{% if loop.first %} | ||
|
||
{% endif %} | ||
{##} {##}{% if e.qualities %}{{e.qualities | idltxt}} {% endif -%} | ||
{{e.priority | idltxt}} event {{e | event_access}}{{e.name}} = {{e.code}} { | ||
{% for field in e.fields %} | ||
{{render_field(field)}} | ||
{% endfor %} | ||
} | ||
{% if not loop.last %} | ||
|
||
{% endif %} | ||
{% endfor %} | ||
|
||
{%- for a in cluster.attributes %} | ||
{% if loop.first %} | ||
|
||
{% endif %} | ||
{{a.qualities | idltxt}}attribute {{a | attribute_access}}{{render_field(a.definition)}} | ||
{% endfor %} | ||
|
||
{%- for s in cluster.structs | selectattr("tag") %} | ||
|
||
{{render_struct(s)}} | ||
{% endfor %} | ||
|
||
{%- for c in cluster.commands %} | ||
{% if loop.first %} | ||
|
||
{% endif %} | ||
{% if c.description %} | ||
/** {{c.description}} */ | ||
{% endif %} | ||
{{c.qualities | idltxt}}command {{c | command_access}}{{c.name}}( | ||
{%- if c.input_param %}{{c.input_param}}{% endif -%} | ||
): {{c.output_param}} = {{c.code}}; | ||
{% endfor %} | ||
} | ||
|
||
{% endfor %} | ||
|
||
{%- if idl.endpoints %} | ||
{%- for endpoint in idl.endpoints %} | ||
endpoint {{endpoint.number}} { | ||
{% for t in endpoint.device_types %} | ||
device type {{t.name}} = {{t.code}}, version {{t.version}}; | ||
{% endfor%} | ||
|
||
{%-for b in endpoint.client_bindings %} | ||
{% if loop.first %} | ||
|
||
{% endif %} | ||
binding cluster {{b}}; | ||
{% endfor %} | ||
|
||
{%-for c in endpoint.server_clusters %} | ||
|
||
server cluster {{c.name}} { | ||
{% for e in c.events_emitted %} | ||
emits event {{e}}; | ||
{% if loop.last %} | ||
|
||
{% endif %} | ||
{% endfor %} | ||
{% for a in c.attributes %} | ||
{{"%-8s" | format(a.storage|idltxt) }} attribute {{a.name}} | ||
{%- if a.default is not none %} default = {{a.default|render_default}} {%- endif %}; | ||
{% endfor %} | ||
{% for cmd in c.commands %} | ||
{% if loop.first %} | ||
|
||
{% endif %} | ||
handle command {{cmd.name}}; | ||
{% endfor %} | ||
} | ||
{% endfor %} | ||
|
||
} | ||
{% if not loop.last %} | ||
|
||
{% endif %} | ||
{% endfor %} | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
## Generator description | ||
|
||
Generates a structured `Idl` data type into a human-readable text format | ||
(`.matter` file). | ||
|
||
It is useful for tools that ingest non-idl data but convert into idl data (e.g. | ||
`zapxml` or CSA data model XML data.) | ||
|
||
### Usage | ||
|
||
A no-op usage can be: | ||
|
||
``` | ||
./scripts/codegen.py -g idl --output-dir out/idlgen examples/all-clusters-app/all-clusters-common/all-clusters-app.matter | ||
``` | ||
|
||
which would re-generate the entire `all-clusters-app.matter` into | ||
`out/idlgen/idl.matter` | ||
|
||
This generation is useful for testing/validating that both parsing and | ||
generation works. Actual usage of this generator would be inside XML tools. | ||
|
||
### Within XML parsing | ||
|
||
A XML parser will use this code generator to output a human readable view of the | ||
parsed data: | ||
|
||
``` | ||
./scripts/py_matter_idl/matter_idl/xml_parser.py \ | ||
./src/app/zap-templates/zcl/data-model/chip/onoff-cluster.xml \ | ||
./src/app/zap-templates/zcl/data-model/chip/global-attributes.xm | ||
``` |
Oops, something went wrong.