Skip to content

Commit

Permalink
[Android] Fix generated struct variable type (#29360)
Browse files Browse the repository at this point in the history
* Fix generated struct variable type

* generated files.gni

* Update Struct Class for avoiding build error

* Update unit test

* update kotlin codestyle

* Add jinja test file

* Update script test code
  • Loading branch information
joonhaengHeo authored and pull[bot] committed Feb 13, 2024
1 parent 08f42c0 commit 3dfc703
Show file tree
Hide file tree
Showing 155 changed files with 1,416 additions and 1,981 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
{{encode_tlv(encodable.without_optional(), tag, "opt" + name, depth + 1)}}
}
{%- elif encodable.is_list -%}
startList({{tag}})
startArray({{tag}})
for (item in {{name}}.iterator()) {
{{encode_tlv(encodable.without_list(), "AnonymousTag", "item", depth + 1)}}
}
endList()
endArray()
{%- elif encodable.is_struct -%}
{{name}}.toTlv({{tag}}, this)
{%- else -%}
Expand All @@ -55,7 +55,7 @@
{%- elif encodable.is_list -%}
{%- set encodablewithoutlist = encodable.without_list() -%}
buildList <{{encode_value(source, encodablewithoutlist, depth + 1)}}> {
tlvReader.enterList({{tag}})
tlvReader.enterArray({{tag}})
while(!tlvReader.isEndOfContainer()) {
this.add({{decode_tlv(source, encodablewithoutlist, "AnonymousTag", depth + 1)}})
}
Expand Down Expand Up @@ -115,9 +115,9 @@ class {{cluster.name}}Cluster{{event.name}}Event (
append("}\n")
}

fun toTlv(tag: Tag, tlvWriter: TlvWriter) {
fun toTlv(tlvTag: Tag, tlvWriter: TlvWriter) {
tlvWriter.apply {
startStructure(tag)
startStructure(tlvTag)
{% for field in event.fields %}
{%- set encodable = field | asEncodable(typeLookup) %}
{%- set tag = contextSpecificTag(field) -%}
Expand All @@ -132,8 +132,8 @@ class {{cluster.name}}Cluster{{event.name}}Event (
private const val TAG_{{field.name | constcase}} = {{field.code}}
{%- endfor %}

fun fromTlv(tag: Tag, tlvReader: TlvReader) : {{cluster.name}}Cluster{{event.name}}Event {
tlvReader.enterStructure(tag)
fun fromTlv(tlvTag: Tag, tlvReader: TlvReader) : {{cluster.name}}Cluster{{event.name}}Event {
tlvReader.enterStructure(tlvTag)
{% for field in event.fields %}
{%- set decodable = field | asEncodable(typeLookup) %}
{%- set tag = contextSpecificTag(field) -%}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

structs_sources = [
{%- for cluster in clientClusters | sort(attribute='name') %}
{%- set typeLookup = idl | createLookupContext(cluster) %}
{%- for struct in cluster.structs | sort(attribute='name') %}
{%- if not struct.tag %}
"${chip_root}/src/controller/java/generated/java/chip/devicecontroller/cluster/structs/{{cluster.name}}Cluster{{struct.name}}.kt",
{%- endif %}
{%- endfor %}
{%- endfor %}
]

eventstructs_sources = [
{%- for cluster in clientClusters | sort(attribute='name') %}
{%- set typeLookup = idl | createLookupContext(cluster) %}
{%- for event in cluster.events | sort(attribute='name') %}
{%- if event.fields %}
"${chip_root}/src/controller/java/generated/java/chip/devicecontroller/cluster/eventstructs/{{cluster.name}}Cluster{{event.name}}Event.kt",
{%- endif %}
{%- endfor %}
{%- endfor %}
]
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
{{encode_tlv(encodable.without_optional(), tag, "opt" + name, depth + 1)}}
}
{%- elif encodable.is_list -%}
startList({{tag}})
startArray({{tag}})
for (item in {{name}}.iterator()) {
{{encode_tlv(encodable.without_list(), "AnonymousTag", "item", depth + 1)}}
}
endList()
endArray()
{%- elif encodable.is_struct -%}
{{name}}.toTlv({{tag}}, this)
{%- else -%}
Expand All @@ -55,7 +55,7 @@
{%- elif encodable.is_list -%}
{%- set encodablewithoutlist = encodable.without_list() -%}
buildList<{{encode_value(source, encodablewithoutlist, depth + 1)}}> {
tlvReader.enterList({{tag}})
tlvReader.enterArray({{tag}})
while(!tlvReader.isEndOfContainer()) {
add({{decode_tlv(source, encodablewithoutlist, "AnonymousTag", depth + 1)}})
}
Expand Down Expand Up @@ -115,9 +115,9 @@ class {{cluster.name}}Cluster{{struct.name}} (
append("}\n")
}

fun toTlv(tag: Tag, tlvWriter: TlvWriter) {
fun toTlv(tlvTag: Tag, tlvWriter: TlvWriter) {
tlvWriter.apply {
startStructure(tag)
startStructure(tlvTag)
{% for field in struct.fields %}
{%- set encodable = field | asEncodable(typeLookup) %}
{%- set tag = contextSpecificTag(field) -%}
Expand All @@ -132,8 +132,8 @@ class {{cluster.name}}Cluster{{struct.name}} (
private const val TAG_{{field.name | constcase}} = {{field.code}}
{%- endfor %}

fun fromTlv(tag: Tag, tlvReader: TlvReader) : {{cluster.name}}Cluster{{struct.name}} {
tlvReader.enterStructure(tag)
fun fromTlv(tlvTag: Tag, tlvReader: TlvReader) : {{cluster.name}}Cluster{{struct.name}} {
tlvReader.enterStructure(tlvTag)
{% for field in struct.fields %}
{%- set decodable = field | asEncodable(typeLookup) %}
{%- set tag = contextSpecificTag(field) -%}
Expand Down
29 changes: 22 additions & 7 deletions scripts/py_matter_idl/matter_idl/generators/java/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,25 +493,31 @@ def kotlin_type(self):
raise Exception("Unknown fundamental type")
elif isinstance(t, BasicInteger):
# the >= 3 will include int24_t to be considered "long"
if t.byte_count >= 3:
return "Long"
if t.is_signed:
if t.byte_count >= 3:
return "Long"
else:
return "Int"
else:
return "Int"
if t.byte_count >= 3:
return "ULong"
else:
return "UInt"
elif isinstance(t, BasicString):
if t.is_binary:
return "ByteArray"
else:
return "String"
elif isinstance(t, IdlEnumType):
if t.base_type.byte_count >= 3:
return "Long"
return "ULong"
else:
return "Int"
return "UInt"
elif isinstance(t, IdlBitmapType):
if t.base_type.byte_count >= 3:
return "Long"
return "ULong"
else:
return "Int"
return "UInt"
else:
return "Any"

Expand Down Expand Up @@ -790,6 +796,15 @@ def internal_render_all(self):
}
)

self.internal_render_one_output(
template_path="ChipStructFiles_gni.jinja",
output_file_name="java/chip/devicecontroller/cluster/files.gni",
vars={
'idl': self.idl,
'clientClusters': clientClusters,
}
)

# Every cluster has its own impl, to avoid
# very large compilations (running out of RAM)
for cluster in self.idl.clusters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ java-class:
java/chip/devicecontroller/ClusterWriteMapping.java: outputs/several_clusters/java/ClusterWriteMapping.java
java/chip/devicecontroller/ClusterReadMapping.java: outputs/several_clusters/java/ClusterReadMapping.java
java/chip/devicecontroller/ClusterIDMapping.java: outputs/several_clusters/java/ClusterIDMapping.java
java/chip/devicecontroller/cluster/files.gni: outputs/several_clusters/java/files.gni

cpp-app:
inputs/several_clusters.matter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import java.util.Optional

class SecondClusterFabricDescriptorStruct (
val rootPublicKey: ByteArray,
val vendorID: Int,
val fabricID: Long,
val nodeID: Long,
val vendorID: UInt,
val fabricID: ULong,
val nodeID: ULong,
val label: String,
val fabricIndex: Int) {
val fabricIndex: UInt) {
override fun toString(): String = buildString {
append("SecondClusterFabricDescriptorStruct {\n")
append("\trootPublicKey : $rootPublicKey\n")
Expand All @@ -44,9 +44,9 @@ class SecondClusterFabricDescriptorStruct (
append("}\n")
}

fun toTlv(tag: Tag, tlvWriter: TlvWriter) {
fun toTlv(tlvTag: Tag, tlvWriter: TlvWriter) {
tlvWriter.apply {
startStructure(tag)
startStructure(tlvTag)
put(ContextSpecificTag(TAG_ROOT_PUBLIC_KEY), rootPublicKey)
put(ContextSpecificTag(TAG_VENDOR_I_D), vendorID)
put(ContextSpecificTag(TAG_FABRIC_I_D), fabricID)
Expand All @@ -65,14 +65,14 @@ class SecondClusterFabricDescriptorStruct (
private const val TAG_LABEL = 5
private const val TAG_FABRIC_INDEX = 254

fun fromTlv(tag: Tag, tlvReader: TlvReader) : SecondClusterFabricDescriptorStruct {
tlvReader.enterStructure(tag)
fun fromTlv(tlvTag: Tag, tlvReader: TlvReader) : SecondClusterFabricDescriptorStruct {
tlvReader.enterStructure(tlvTag)
val rootPublicKey = tlvReader.getByteArray(ContextSpecificTag(TAG_ROOT_PUBLIC_KEY))
val vendorID = tlvReader.getInt(ContextSpecificTag(TAG_VENDOR_I_D))
val fabricID = tlvReader.getLong(ContextSpecificTag(TAG_FABRIC_I_D))
val nodeID = tlvReader.getLong(ContextSpecificTag(TAG_NODE_I_D))
val vendorID = tlvReader.getUInt(ContextSpecificTag(TAG_VENDOR_I_D))
val fabricID = tlvReader.getULong(ContextSpecificTag(TAG_FABRIC_I_D))
val nodeID = tlvReader.getULong(ContextSpecificTag(TAG_NODE_I_D))
val label = tlvReader.getString(ContextSpecificTag(TAG_LABEL))
val fabricIndex = tlvReader.getInt(ContextSpecificTag(TAG_FABRIC_INDEX))
val fabricIndex = tlvReader.getUInt(ContextSpecificTag(TAG_FABRIC_INDEX))

tlvReader.exitContainer()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

structs_sources = [
"${chip_root}/src/controller/java/generated/java/chip/devicecontroller/cluster/structs/SecondClusterFabricDescriptorStruct.kt",
]

eventstructs_sources = [
]
2 changes: 1 addition & 1 deletion src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ kotlin_library("onboardingpayload_qr_code_test") {

kotlin_library("chipcluster") {
import(
"${chip_root}/src/controller/java/src/chip/devicecontroller/cluster/files.gni")
"${chip_root}/src/controller/java/generated/java/chip/devicecontroller/cluster/files.gni")

output_name = "CHIPClusters.jar"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import chip.tlv.TlvReader
import chip.tlv.TlvWriter

class AccessControlClusterAccessControlEntryChangedEvent(
val adminNodeID: Long?,
val adminPasscodeID: Int?,
val changeType: Int,
val adminNodeID: ULong?,
val adminPasscodeID: UInt?,
val changeType: UInt,
val latestValue:
chip.devicecontroller.cluster.structs.AccessControlClusterAccessControlEntryStruct?,
val fabricIndex: Int
val fabricIndex: UInt
) {
override fun toString(): String = buildString {
append("AccessControlClusterAccessControlEntryChangedEvent {\n")
Expand All @@ -40,9 +40,9 @@ class AccessControlClusterAccessControlEntryChangedEvent(
append("}\n")
}

fun toTlv(tag: Tag, tlvWriter: TlvWriter) {
fun toTlv(tlvTag: Tag, tlvWriter: TlvWriter) {
tlvWriter.apply {
startStructure(tag)
startStructure(tlvTag)
if (adminNodeID != null) {
put(ContextSpecificTag(TAG_ADMIN_NODE_I_D), adminNodeID)
} else {
Expand Down Expand Up @@ -72,25 +72,25 @@ class AccessControlClusterAccessControlEntryChangedEvent(
private const val TAG_FABRIC_INDEX = 254

fun fromTlv(
tag: Tag,
tlvTag: Tag,
tlvReader: TlvReader
): AccessControlClusterAccessControlEntryChangedEvent {
tlvReader.enterStructure(tag)
tlvReader.enterStructure(tlvTag)
val adminNodeID =
if (!tlvReader.isNull()) {
tlvReader.getLong(ContextSpecificTag(TAG_ADMIN_NODE_I_D))
tlvReader.getULong(ContextSpecificTag(TAG_ADMIN_NODE_I_D))
} else {
tlvReader.getNull(ContextSpecificTag(TAG_ADMIN_NODE_I_D))
null
}
val adminPasscodeID =
if (!tlvReader.isNull()) {
tlvReader.getInt(ContextSpecificTag(TAG_ADMIN_PASSCODE_I_D))
tlvReader.getUInt(ContextSpecificTag(TAG_ADMIN_PASSCODE_I_D))
} else {
tlvReader.getNull(ContextSpecificTag(TAG_ADMIN_PASSCODE_I_D))
null
}
val changeType = tlvReader.getInt(ContextSpecificTag(TAG_CHANGE_TYPE))
val changeType = tlvReader.getUInt(ContextSpecificTag(TAG_CHANGE_TYPE))
val latestValue =
if (!tlvReader.isNull()) {
chip.devicecontroller.cluster.structs.AccessControlClusterAccessControlEntryStruct
Expand All @@ -99,7 +99,7 @@ class AccessControlClusterAccessControlEntryChangedEvent(
tlvReader.getNull(ContextSpecificTag(TAG_LATEST_VALUE))
null
}
val fabricIndex = tlvReader.getInt(ContextSpecificTag(TAG_FABRIC_INDEX))
val fabricIndex = tlvReader.getUInt(ContextSpecificTag(TAG_FABRIC_INDEX))

tlvReader.exitContainer()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import chip.tlv.TlvReader
import chip.tlv.TlvWriter

class AccessControlClusterAccessControlExtensionChangedEvent(
val adminNodeID: Long?,
val adminPasscodeID: Int?,
val changeType: Int,
val adminNodeID: ULong?,
val adminPasscodeID: UInt?,
val changeType: UInt,
val latestValue:
chip.devicecontroller.cluster.structs.AccessControlClusterAccessControlExtensionStruct?,
val fabricIndex: Int
val fabricIndex: UInt
) {
override fun toString(): String = buildString {
append("AccessControlClusterAccessControlExtensionChangedEvent {\n")
Expand All @@ -40,9 +40,9 @@ class AccessControlClusterAccessControlExtensionChangedEvent(
append("}\n")
}

fun toTlv(tag: Tag, tlvWriter: TlvWriter) {
fun toTlv(tlvTag: Tag, tlvWriter: TlvWriter) {
tlvWriter.apply {
startStructure(tag)
startStructure(tlvTag)
if (adminNodeID != null) {
put(ContextSpecificTag(TAG_ADMIN_NODE_I_D), adminNodeID)
} else {
Expand Down Expand Up @@ -72,25 +72,25 @@ class AccessControlClusterAccessControlExtensionChangedEvent(
private const val TAG_FABRIC_INDEX = 254

fun fromTlv(
tag: Tag,
tlvTag: Tag,
tlvReader: TlvReader
): AccessControlClusterAccessControlExtensionChangedEvent {
tlvReader.enterStructure(tag)
tlvReader.enterStructure(tlvTag)
val adminNodeID =
if (!tlvReader.isNull()) {
tlvReader.getLong(ContextSpecificTag(TAG_ADMIN_NODE_I_D))
tlvReader.getULong(ContextSpecificTag(TAG_ADMIN_NODE_I_D))
} else {
tlvReader.getNull(ContextSpecificTag(TAG_ADMIN_NODE_I_D))
null
}
val adminPasscodeID =
if (!tlvReader.isNull()) {
tlvReader.getInt(ContextSpecificTag(TAG_ADMIN_PASSCODE_I_D))
tlvReader.getUInt(ContextSpecificTag(TAG_ADMIN_PASSCODE_I_D))
} else {
tlvReader.getNull(ContextSpecificTag(TAG_ADMIN_PASSCODE_I_D))
null
}
val changeType = tlvReader.getInt(ContextSpecificTag(TAG_CHANGE_TYPE))
val changeType = tlvReader.getUInt(ContextSpecificTag(TAG_CHANGE_TYPE))
val latestValue =
if (!tlvReader.isNull()) {
chip.devicecontroller.cluster.structs.AccessControlClusterAccessControlExtensionStruct
Expand All @@ -99,7 +99,7 @@ class AccessControlClusterAccessControlExtensionChangedEvent(
tlvReader.getNull(ContextSpecificTag(TAG_LATEST_VALUE))
null
}
val fabricIndex = tlvReader.getInt(ContextSpecificTag(TAG_FABRIC_INDEX))
val fabricIndex = tlvReader.getUInt(ContextSpecificTag(TAG_FABRIC_INDEX))

tlvReader.exitContainer()

Expand Down
Loading

0 comments on commit 3dfc703

Please sign in to comment.