Skip to content

Commit

Permalink
add event codegen for python (#11802)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google authored and pull[bot] committed Jan 9, 2023
1 parent 5aaa061 commit 1942089
Show file tree
Hide file tree
Showing 5 changed files with 586 additions and 68 deletions.
4 changes: 2 additions & 2 deletions src/app/zap-templates/templates/app/cluster-objects.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ enum class Fields {
struct Type
{
public:
static constexpr PriorityLevel priorityLevel = PriorityLevel::{{asUpperCamelCase priority}};
static constexpr EventId eventId = {{asMEI manufacturerCode code}};
static constexpr PriorityLevel GetPriorityLevel() { return kPriorityLevel; }
static constexpr EventId GetEventId() { return kEventId; }
static constexpr ClusterId GetClusterId() { return Clusters::{{asUpperCamelCase parent.name}}::Id; }

{{#zcl_event_fields}}
Expand Down
56 changes: 56 additions & 0 deletions src/controller/python/chip/clusters/ClusterObjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,59 @@ def _cluster_object(cls) -> ClusterObject:
)
],
bases=(ClusterObject,))


class ClusterEventDescriptor:
'''
The ClusterEventDescriptor is used for holding an event's metadata like its cluster id, event id and its type.
Users should not initialize an object based on this class. Instead, users should pass the subclass objects to tell some methods what they want.
The implementation of this functions is quite tricky, it will create a cluster object on-the-fly, and use it for actual encode / decode routine to save lines of code.
'''
@classmethod
def ToTLV(cls, tag: Union[int, None], value):
writer = tlv.TLVWriter()
wrapped_value = cls._cluster_object(Value=value)
cls.event_type.PutFieldToTLV(tag,
asdict(wrapped_value)['Value'], writer, '')
return writer.encoding

@classmethod
def FromTLV(cls, tlvBuffer: bytes):
obj_class = cls._cluster_object
return obj_class.FromDict(obj_class.descriptor.TagDictToLabelDict('', {0: tlv.TLVReader(tlvBuffer).get().get('Any', {})})).Value

@classmethod
def FromTagDictOrRawValue(cls, val: Any):
obj_class = cls._cluster_object
return obj_class.FromDict(obj_class.descriptor.TagDictToLabelDict('', {0: val})).Value

@ChipUtility.classproperty
def cluster_id(self) -> int:
raise NotImplementedError()

@ChipUtility.classproperty
def event_id(self) -> int:
raise NotImplementedError()

@ChipUtility.classproperty
def event_type(cls) -> ClusterObjectFieldDescriptor:
raise NotImplementedError()

@ChipUtility.classproperty
def _cluster_object(cls) -> ClusterObject:
return make_dataclass('InternalClass',
[
('Value', List[cls.event_type.Type]
if cls.event_type.IsArray else cls.event_type.Type, field(default=None)),
('descriptor', ClassVar[ClusterObjectDescriptor],
field(
default=ClusterObjectDescriptor(
Fields=[ClusterObjectFieldDescriptor(
Label='Value', Tag=0, Type=cls.event_type.Type, IsArray=cls.event_type.IsArray)]
)
)
)
],
bases=(ClusterObject,))
Loading

0 comments on commit 1942089

Please sign in to comment.