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

Matter improve encoding of attributes to reduce flash size #21864

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions lib/libesp32/berry_matter/src/embedded/Matter_0_Inspect.be
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,35 @@ matter.inspect = inspect
# from the inheritance hierarchy
#@ solidify:matter.consolidate_clusters,weak
def consolidate_clusters(cl, m)
var cl_parent = super(cl) != nil ? super(cl).CLUSTERS : {}
var cl_parent = (super(cl) != nil) ? super(cl).CLUSTERS : {}
var ret = {}
# clone cl_parent
for k: cl_parent.keys()
# print(f"{k=} {cl_parent[k]=}")
ret[k] = cl_parent[k].copy()
# rebuild an actual list
var attr_arr = []
var attr_bytes = cl_parent[k]
var attr_bytes_sz = (attr_bytes != nil) ? size(attr_bytes) / 2 : 0
var idx = 0
while (idx < attr_bytes_sz)
attr_arr.push(attr_bytes.get(idx * 2, -2))
idx += 1
end
ret[k] = attr_arr
# ret[k] = cl_parent[k].copy()
end
# add all keys from m
# print("--- step 2")
for k: m.keys()
# print(f"{k=} {ret.find(k)=} {m[k]=}")
var l = ret.find(k)
if l == nil l = [] end
ret[k] = l + m[k]
if !ret.contains(k)
ret[k] = []
end
for v: m[k]
if ret[k].find(v) == nil
ret[k].push(v)
end
end
end
# add all auto-attribbutes to each cluster
var AUTO_ATTRIBUTES = [ # pre-defined auto attributes for every cluster
Expand All @@ -120,6 +135,23 @@ def consolidate_clusters(cl, m)
end
end
end
# sort all lists
for k: ret.keys()
var attr_list = ret[k]
# sort in place
ret[k] = matter.sort(attr_list)
end
# convert back to bytes
for k: ret.keys()
var attr_arr = ret[k]
var attr_bytes = bytes()
var idx = 0
while (idx < size(attr_arr))
attr_bytes.add(attr_arr[idx], -2)
idx += 1
end
ret[k] = attr_bytes
end
# print(ret)
return ret
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,17 +266,32 @@ class Matter_PathGenerator
def _next_attribute()
if (self.attribute == false) return false end # exhausted all possible values

var attributes = self.pi.get_attribute_list(self.cluster)
var attributes_bytes = self.pi.get_attribute_list_bytes(self.cluster)
var attributes_bytes_sz = (attributes_bytes != nil) ? size(attributes_bytes) / 2 : 0
var attr_filter = self.path_in_attribute

var idx = -1
if (self.attribute != nil)
idx = attributes.find(self.attribute) # find index in current list
var i = 0
while true
if (i < attributes_bytes_sz)
if attributes_bytes.get(i * 2, -2) == self.attribute
idx = i
break
end
i += 1
else
idx = nil
break
end
end
# idx = attributes.find(self.attribute) # finds index in current list
end
# safeguard
if (idx != nil)
while (idx + 1 < size(attributes))
while (idx + 1 < attributes_bytes_sz)
idx += 1 # move to next item
self.attribute = attributes[idx]
self.attribute = attributes_bytes.get(idx * 2, -2)
if (attr_filter == nil) || (attr_filter == self.attribute)
return self.attribute
end
Expand Down
29 changes: 17 additions & 12 deletions lib/libesp32/berry_matter/src/embedded/Matter_Plugin_0.be
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ class Matter_Plugin
static var UPDATE_COMMANDS = []
var device # reference to the `device` global object
var endpoint # current endpoint
var clusters # map from cluster to list of attributes, typically constructed from CLUSTERS hierachy
var tick # tick value when it was last updated
var node_label # name of the endpoint, used only in bridge mode, "" if none

Expand All @@ -118,7 +117,6 @@ class Matter_Plugin
def init(device, endpoint, config)
self.device = device
self.endpoint = endpoint
self.clusters = self.get_clusters()
self.parse_configuration(config)
self.node_label = config.find("name", "")
end
Expand Down Expand Up @@ -277,20 +275,26 @@ matter_device.events.dump()
return self.endpoint
end
def get_cluster_list_sorted()
return self.device.k2l(self.clusters)
return self.device.k2l(self.CLUSTERS)
end
def contains_cluster(cluster)
return self.clusters.contains(cluster)
return self.CLUSTERS.contains(cluster)
end
def get_attribute_list(cluster)
return self.clusters.find(cluster, [])
# def get_attribute_list(cluster)
# return self.clusters.find(cluster, [])
# end
# returns as a constant bytes of 16-bit ints, big endian
def get_attribute_list_bytes(cluster)
return self.CLUSTERS.find(cluster, nil)
end
def contains_attribute(cluster, attribute)
var attr_list = self.clusters.find(cluster)
var attr_list = self.CLUSTERS.find(cluster)
# log(f"MTR: contains_attribute {cluster=} {attribute=} {attr_list=}")
if attr_list != nil
var idx = 0
while idx < size(attr_list)
if attr_list[idx] == attribute
var attr_sz = size(attr_list) / 2 # group of 16-bit integers, big endian
while idx < attr_sz
if attr_list.get(idx * 2, -2) == attribute
return true
end
idx += 1
Expand Down Expand Up @@ -359,10 +363,11 @@ matter_device.events.dump()
return gcl # return empty list
elif attribute == 0xFFFB # AttributeList
var acli = TLV.Matter_TLV_array()
var attr_list = self.get_attribute_list(cluster)
var attr_list_bytes = self.get_attribute_list_bytes(cluster)
var attr_list_bytes_sz = (attr_list_bytes != nil) ? size(attr_list_bytes) : 0
var idx = 0
while idx < size(attr_list)
acli.add_TLV(nil, TLV.U2, attr_list[idx])
while idx < attr_list_bytes_sz
acli.add_TLV(nil, TLV.U2, attr_list_bytes.get(idx * 2, -2))
idx += 1
end
return acli # TODO, empty list for now
Expand Down
Loading