Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for typedef #1458

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open

Add support for typedef #1458

wants to merge 18 commits into from

Conversation

gmarcosb
Copy link

@gmarcosb gmarcosb commented Oct 17, 2024

Adds support for typedef in zap, supporting things like defining a VideoStreamID as a uint16, see the camera spec: https://github.com/CHIP-Specifications/connectedhomeip-spec/pull/10004

See also Matter SDK template changes @ project-chip/connectedhomeip#36124

This should also eliminate the need for many of the things in chip-types.xml, which can now be simply defined in the XML files as a simple typedef

See also discussion with @bzbarsky-apple: project-chip/connectedhomeip#35773 (comment)

Copy link
Collaborator

@paulr34 paulr34 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good and thorough. It would be nice if you could fix the schema format to be consistent.

src-electron/db/zap-schema.sql Show resolved Hide resolved
@gmarcosb
Copy link
Author

@brdandu or @tecimovic could I get one of you to review? Thank you!

@brdandu
Copy link
Collaborator

brdandu commented Nov 4, 2024

I am wondering if this is very similar to base type implementation in #1472 . Do you think that can work the same way as your implementation?

@gmarcosb
Copy link
Author

gmarcosb commented Nov 4, 2024

I am wondering if this is very similar to base type implementation in #1472 . Do you think that can work the same way as your implementation?

This seems to be a superset of that - in fact, this new typedef should probably supercede many of the base types - for example, in src/app/zap-templates/zcl/data-model/chip/chip-types.xml, cluster_id, field_id could instead use the typedef here

Not only does this PR allow for block helpers, it also allows for dynamic specifying of simple types alongside structs/etc. instead of having to have a definition in chip-types.xml as well as the code definitions - it's a lot more elegant

@brdandu
Copy link
Collaborator

brdandu commented Nov 5, 2024

I am wondering if this is very similar to base type implementation in #1472 . Do you think that can work the same way as your implementation?

This seems to be a superset of that - in fact, this new typedef should probably supercede many of the base types - for example, in src/app/zap-templates/zcl/data-model/chip/chip-types.xml, cluster_id, field_id could instead use the typedef here

Not only does this PR allow for block helpers, it also allows for dynamic specifying of simple types alongside structs/etc. instead of having to have a definition in chip-types.xml as well as the code definitions - it's a lot more elegant

I do see some of the benefits of having a typedef implementation for better spec readability. Here a couple of things I am thinking from the top of my head and worried about:

  • The atomic identifier won't be associated with these types. Is that fine?
  • Also as of today each type is made unique as per one of the types i.e. enum, bitmap, struct or number(discriminator). So when type def comes in then does it also mean it will be a typedef and something else?
  • If the attribute types and command argument types start using the typedefs then do our existing helpers need to take that into account?

Should we have a corresponding chip repo PR which uses this to validate some of this behavior? @bzbarsky-apple @andy31415 do you guys have any input here?

@gmarcosb
Copy link
Author

gmarcosb commented Nov 5, 2024

The atomic identifier won't be associated with these types. Is that fine:

I think so? I think we'd want to move some of the types currently defined in chip-types.xml to just be simple typedefs, so that it's not split between XML + code; but I may not be understanding what the detriment would be of not being associated with the atomic identifier

So when type def comes in then does it also mean it will be a typedef and something else?

Assuming I understand your question: typedef is treated exactly like enum & struct, i.e. the name is added to its own SQL table & it is checked for uniqueness against all types

do our existing helpers need to take that into account?

Yes, although in general there are shared functions - for example, when we lookup the type to output in code, we just do a redirect to the underlying type

@brdandu
Copy link
Collaborator

brdandu commented Nov 5, 2024

For reference, corresponding chip repo PR: project-chip/connectedhomeip#36124

@brdandu
Copy link
Collaborator

brdandu commented Nov 5, 2024

I think so? I think we'd want to move some of the types currently defined in chip-types.xml to just be simple typedefs, so that it's not split between XML + code; but I may not be understanding what the detriment would be of not being associated with the atomic identifier

I could be wrong here but I believe we pass the atomic identifier in our zcl read and write attribute calls if I am not wrong. It is worth checking that our generated code continues to extract the right atomic identifier here when the typedef is introduced to the attribute types and this continues to work the same way:
Eg for Test: Turn attribute type to a type def in xml. Then in you built application try to read and write this attribute to make sure this is working.

Assuming I understand your question: typedef is treated exactly like enum & struct, i.e. the name is added to its own SQL table & it is checked for uniqueness against all types

In our templates we very frequently pass the attribute type to a bunch of helpers for processing. What happens to the those helpers when the typedef is passed?
Eg for test: Turn attribute type to a type def in xml. Now in the .zapt templates look for helpers like as_underlying_zcl_type/asUnderlyingZclType or as_underlying_type/asUnderlyingType which takes in the attribute type and gives a certain type for it. Check if this is behaving appropriately for typedefs

Yes, although in general there are shared functions - for example, when we lookup the type to output in code, we just do a redirect to the underlying type

Can this be verified by adding more unit tests here based on the above example?

@gmarcosb
Copy link
Author

gmarcosb commented Nov 5, 2024

I added tests such that they are 1:1 with the enum & struct tests

Unfortunately, the tests are somewhat split between here & the matter SDK repo

Here, the tests just check for the basic functionality: i.e. if we find a typedef in a template, does it generate the right output?

Those tests here are updated to handle typedefs & pass

Then, in the Matter SDK repo, there are numerous other tests around the zap templates there (including the compiler itself - i.e. if the generated files compile)

Those tests are not yet complete, but are more testing the templates rather than ZAP itself (I am working on those now, though some are complete)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants