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

stop using controller-clusters.zap in zap_generate_all #26948

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
af62ac7
separate out zap input, so we have control over it
andreilitvin May 18, 2023
d62d21e
Switch zcl_clusters on for python
andreilitvin May 18, 2023
6847e9d
Fix more loops to use the generic non-zap helpers
andreilitvin May 18, 2023
9c71634
Merge branch 'use_zcl_clusters_for_python' into remove_controller_clu…
andreilitvin May 18, 2023
f19df92
Switch zap generate to use zcl
andreilitvin May 18, 2023
f48d59a
update generate.py and change CHIPClientCallbacks.zapt
andreilitvin May 18, 2023
75ff7a7
Merge branch 'master' into remove_controller_clusters_zap
andy31415 May 19, 2023
ec1735e
Merge branch 'master' into remove_controller_clusters_zap
andreilitvin May 30, 2023
63d2bf7
Restyled by autopep8
restyled-commits May 30, 2023
784966e
Fix another comment location
andreilitvin May 30, 2023
6fed326
Remove import from optional to make linter happy
andreilitvin May 30, 2023
946e996
Skip restyling java generated files
andreilitvin May 30, 2023
9d58f8d
Run zap_regen_all
andreilitvin May 30, 2023
f24c21f
Fix restyle path to include a glob
andreilitvin May 30, 2023
13ae5c0
Merge branch 'no_java_generated_prettify' into remove_controller_clus…
andreilitvin May 30, 2023
4ebb53a
Merge branch 'master' into remove_controller_clusters_zap
andreilitvin May 30, 2023
3af2ff1
Switch global callbacks to jinja
andreilitvin May 30, 2023
fcab977
update unit tests
andreilitvin May 30, 2023
3d4b59a
Fix global logic and codegen
andreilitvin May 30, 2023
b99c507
Codegen updated, validated that global callbacks is now identical
andreilitvin May 30, 2023
9e455aa
Fix lint issue
andreilitvin May 30, 2023
a33dbfc
Update codegen logic to not depend on zap for src/controller/java/tem…
andreilitvin May 30, 2023
521e377
Also fix src/controller/java/templates/ChipClusters-java.zapt
andreilitvin May 30, 2023
4e188d3
Use ANDROID_NDK_HOME env even for newer gradle build as the environme…
andreilitvin May 30, 2023
049fbb9
Merge branch 'master' into remove_controller_clusters_zap
andreilitvin May 30, 2023
d496a75
Fixed indent a bit
andy31415 May 31, 2023
60bde26
Updated comments a bit about controller-clusters.zap
andy31415 May 31, 2023
bc91d80
Merge branch 'master' into remove_controller_clusters_zap
andy31415 May 31, 2023
daa6abf
Merge branch 'master' into remove_controller_clusters_zap
andy31415 Jun 26, 2023
870e04e
Be explicit on matter file name on codegen
andy31415 Jun 26, 2023
a750158
Remove duplicate output name
andy31415 Jun 26, 2023
5d03a62
Fix file path for CHIPClientCallbacks.h
andy31415 Jun 26, 2023
1f748de
Update scripts/tools/zap_regen_all.py
andy31415 Jun 27, 2023
8e31773
Do some renames and code review updates
andy31415 Jun 27, 2023
5b57f6b
Merge branch 'master' into remove_controller_clusters_zap
andy31415 Jun 27, 2023
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
1 change: 1 addition & 0 deletions examples/android/CHIPTest/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ println 'matterUTestLib='+matterUTestLib
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
ndkPath System.getenv("ANDROID_NDK_HOME")

defaultConfig {
applicationId "com.tcl.chip.chiptest"
Expand Down
1 change: 1 addition & 0 deletions examples/android/CHIPTool/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ apply plugin: 'kotlin-parcelize'

android {
compileSdkVersion 31
ndkPath System.getenv("ANDROID_NDK_HOME")

ndkVersion "23.2.8568313"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <jni.h>
#include <jni/CHIPReadCallbacks.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/SafeInt.h>
#include <platform/PlatformManager.h>
#include <controller/java/zap-generated/CHIPClientCallbacks.h>

{% for type in globalTypes -%}
{%- set typeLookup = idl | createLookupContext(None) -%}
{%- set encodable = type.idl_type | globalAsEncodable(typeLookup) -%}
CHIP{{type.name}}AttributeCallback::CHIP{{type.name}}AttributeCallback(jobject javaCallback, bool keepAlive) :
chip::Callback::Callback<{{type.name}}AttributeCallback>(CallbackFn, this), keepAlive(keepAlive)
{
JNIEnv * env = chip::JniReferences::GetInstance().GetEnvForCurrentThread();
if (env == nullptr) {
ChipLogError(Zcl, "Could not create global reference for Java callback");
return;
}
javaCallbackRef = env->NewGlobalRef(javaCallback);
if (javaCallbackRef == nullptr) {
ChipLogError(Zcl, "Could not create global reference for Java callback");
}
}

CHIP{{type.name}}AttributeCallback::~CHIP{{type.name}}AttributeCallback() {
JNIEnv * env = chip::JniReferences::GetInstance().GetEnvForCurrentThread();
if (env == nullptr)
{
ChipLogError(Zcl, "Could not delete global reference for Java callback");
return;
}
env->DeleteGlobalRef(javaCallbackRef);
}

void CHIP{{type.name}}AttributeCallback::CallbackFn(void * context, {{type.cpp_type}} value)
{
chip::DeviceLayer::StackUnlock unlock;
CHIP_ERROR err = CHIP_NO_ERROR;

JNIEnv * env = chip::JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Could not get JNI env"));

std::unique_ptr<CHIP{{type.name}}AttributeCallback, decltype(&maybeDestroy)> cppCallback(reinterpret_cast<CHIP{{type.name}}AttributeCallback *>(context), maybeDestroy);

// It's valid for javaCallbackRef to be nullptr if the Java code passed in a
// null callback.
jobject javaCallbackRef = cppCallback.get()->javaCallbackRef;
VerifyOrReturn(javaCallbackRef != nullptr, ChipLogDetail(Zcl, "Early return from attribute callback since Java callback is null"));

jmethodID javaMethod;
{%- if encodable.is_octet_string %}
err = chip::JniReferences::GetInstance().FindMethod(env, javaCallbackRef, "onSuccess", "([B)V", &javaMethod);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Zcl, "Could not find onSuccess method"));

VerifyOrReturn(chip::CanCastTo<uint32_t>(value.size()), ChipLogError(Zcl, "Value too long"));
jbyteArray valueArr = env->NewByteArray(static_cast<uint32_t>(value.size()));
env->ExceptionClear();
env->SetByteArrayRegion(valueArr, 0, static_cast<uint32_t>(value.size()), reinterpret_cast<const jbyte *>(value.data()));

env->CallVoidMethod(javaCallbackRef, javaMethod, valueArr);
{%- elif encodable.is_char_string %}
err = chip::JniReferences::GetInstance().FindMethod(env, javaCallbackRef, "onSuccess", "(Ljava/lang/String;)V", &javaMethod);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Zcl, "Could not find onSuccess method"));

chip::UtfString valueStr(env, value);
env->CallVoidMethod(javaCallbackRef, javaMethod, valueStr.jniValue());
{%- else %}
err = chip::JniReferences::GetInstance().FindMethod(env, javaCallbackRef, "onSuccess", "({{encodable.unboxed_java_signature}})V", &javaMethod);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Zcl, "Could not find onSuccess method"));
env->CallVoidMethod(javaCallbackRef, javaMethod, static_cast<{{encodable.jni_fundamental_type}}>(value));
{%- endif %}
}

{% endfor %}
84 changes: 68 additions & 16 deletions scripts/py_matter_idl/matter_idl/generators/java/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,26 @@ class GenerateTarget:

@dataclasses.dataclass
class GlobalType:
name: str # java name
name: str # java name
cpp_type: str # underlying type
idl_type: str # assumed IDL type


# types that java should see globally
_GLOBAL_TYPES = [
GlobalType("Boolean", "bool"),
GlobalType("CharString", "const chip::CharSpan"),
GlobalType("Double", "double"),
GlobalType("Float", "float"),
GlobalType("Int8s", "int8_t"),
GlobalType("Int8u", "uint8_t"),
GlobalType("Int16s", "int16_t"),
GlobalType("Int16u", "uint16_t"),
GlobalType("Int32s", "int32_t"),
GlobalType("Int32u", "uint32_t"),
GlobalType("Int64s", "int64_t"),
GlobalType("Int64u", "uint64_t"),
GlobalType("OctetString", "const chip::ByteSpan"),
GlobalType("Boolean", "bool", "boolean"),
GlobalType("CharString", "const chip::CharSpan", "char_string"),
GlobalType("Double", "double", "double"),
GlobalType("Float", "float", "single"),
GlobalType("Int8s", "int8_t", "int8s"),
GlobalType("Int8u", "uint8_t", "int8u"),
GlobalType("Int16s", "int16_t", "int16s"),
GlobalType("Int16u", "uint16_t", "int16u"),
GlobalType("Int32s", "int32_t", "int32s"),
GlobalType("Int32u", "uint32_t", "int32u"),
GlobalType("Int64s", "int64_t", "int64s"),
GlobalType("Int64u", "uint64_t", "int64u"),
GlobalType("OctetString", "const chip::ByteSpan", "octet_string"),
]


Expand Down Expand Up @@ -423,6 +424,23 @@ def get_underlying_enum(self):
raise Exception("Enum %s not found" % self.data_type.name)
return e

@property
def jni_fundamental_type(self):
java_type = self.boxed_java_type

if java_type == 'Boolean':
return 'jboolean'
elif java_type == 'Float':
return 'jfloat'
elif java_type == 'Double':
return 'jdouble'
elif java_type == 'Long':
return 'jlong'
elif java_type == 'Integer':
return 'jint'

raise Exception("Unknown jni fundamental type.")

@property
def boxed_java_type(self):
t = ParseDataType(self.data_type, self.context)
Expand Down Expand Up @@ -460,6 +478,30 @@ def boxed_java_type(self):
else:
return "Object"

@property
def unboxed_java_signature(self):
if self.is_optional or self.is_list:
raise Exception("Not a basic type: %r" % self)

t = ParseDataType(self.data_type, self.context)

if isinstance(t, FundamentalType):
if t == FundamentalType.BOOL:
return "Z"
elif t == FundamentalType.FLOAT:
return "F"
elif t == FundamentalType.DOUBLE:
return "D"
else:
raise Exception("Unknown fundamental type")
elif isinstance(t, BasicInteger):
if t.byte_count >= 3:
return "J"
else:
return "I"
else:
raise Exception("Not a basic type: %r" % self)

@property
def boxed_java_signature(self):
# Optional takes precedence over list - Optional<ArrayList> compiles down to just java.util.Optional.
Expand Down Expand Up @@ -504,6 +546,13 @@ def boxed_java_signature(self):
return "Lchip/devicecontroller/ChipStructs${}Cluster{};".format(self.context.cluster.name, self.data_type.name)


def GlobalEncodableValueFrom(typeName: str, context: TypeLookupContext) -> EncodableValue:
"""
Filter to convert a global type name to an encodable value
"""
return EncodableValue(context, DataType(name=typeName), {})


def EncodableValueFrom(field: Field, context: TypeLookupContext) -> EncodableValue:
"""
Filter to convert a standard field to an EncodableValue.
Expand All @@ -526,7 +575,7 @@ def EncodableValueFrom(field: Field, context: TypeLookupContext) -> EncodableVal
return EncodableValue(context, field.data_type, attrs)


def CreateLookupContext(idl: Idl, cluster: Cluster) -> TypeLookupContext:
def CreateLookupContext(idl: Idl, cluster: Optional[Cluster]) -> TypeLookupContext:
"""
A filter to mark a lookup context to be within a specific cluster.

Expand Down Expand Up @@ -579,6 +628,7 @@ def __init__(self, storage: GeneratorStorage, idl: Idl, **kargs):
self.jinja_env.filters['toBoxedJavaType'] = ToBoxedJavaType
self.jinja_env.filters['lowercaseFirst'] = LowercaseFirst
self.jinja_env.filters['asEncodable'] = EncodableValueFrom
self.jinja_env.filters['globalAsEncodable'] = GlobalEncodableValueFrom
self.jinja_env.filters['createLookupContext'] = CreateLookupContext
self.jinja_env.filters['canGenerateSubscribe'] = CanGenerateSubscribe
self.jinja_env.filters['decodableJniType'] = DecodableJniType
Expand All @@ -602,7 +652,9 @@ def internal_render_all(self):
GenerateTarget(template="CHIPCallbackTypes.jinja",
output_name="jni/CHIPCallbackTypes.h"),
GenerateTarget(template="CHIPReadCallbacks_h.jinja",
output_name="jni/CHIPReadCallbacks.h")
output_name="jni/CHIPReadCallbacks.h"),
GenerateTarget(template="CHIPGlobalCallbacks_cpp.jinja",
output_name="jni/CHIPGlobalCallbacks.cpp"),
]

for target in large_targets:
Expand Down
5 changes: 5 additions & 0 deletions scripts/py_matter_idl/matter_idl/tests/available_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ java-jni:
jni/MyClusterClient-InvokeSubscribeImpl.cpp: outputs/simple_attribute/jni/MyClusterClient-InvokeSubscribeImpl.cpp
jni/CHIPCallbackTypes.h: outputs/simple_attribute/jni/CHIPCallbackTypes.h
jni/CHIPReadCallbacks.h: outputs/simple_attribute/jni/CHIPReadReadCallbacks.h
jni/CHIPGlobalCallbacks.cpp: outputs/shared/jni/CHIPGlobalCallbacks.cpp

inputs/global_struct_attribute.matter:
jni/DemoClusterClient-ReadImpl.cpp: outputs/global_struct_attribute/jni/DemoClusterClient-ReadImpl.cpp
jni/DemoClusterClient-InvokeSubscribeImpl.cpp: outputs/global_struct_attribute/jni/DemoClusterClient-InvokeSubscribeImpl.cpp
jni/CHIPCallbackTypes.h: outputs/global_struct_attribute/jni/CHIPCallbackTypes.h
jni/CHIPReadCallbacks.h: outputs/global_struct_attribute/jni/CHIPReadReadCallbacks.h
jni/CHIPGlobalCallbacks.cpp: outputs/shared/jni/CHIPGlobalCallbacks.cpp

inputs/cluster_struct_attribute.matter:
jni/DemoClusterClient-ReadImpl.cpp: outputs/cluster_struct_attribute/jni/DemoClusterClient-ReadImpl.cpp
jni/DemoClusterClient-InvokeSubscribeImpl.cpp: outputs/cluster_struct_attribute/jni/DemoClusterClient-InvokeSubscribeImpl.cpp
jni/CHIPCallbackTypes.h: outputs/cluster_struct_attribute/jni/CHIPCallbackTypes.h
jni/CHIPReadCallbacks.h: outputs/cluster_struct_attribute/jni/CHIPReadReadCallbacks.h
jni/CHIPGlobalCallbacks.cpp: outputs/shared/jni/CHIPGlobalCallbacks.cpp

inputs/several_clusters.matter:
jni/FirstClient-ReadImpl.cpp: outputs/several_clusters/jni/FirstClient-ReadImpl.cpp
Expand All @@ -38,12 +41,14 @@ java-jni:
jni/ThirdClient-InvokeSubscribeImpl.cpp: outputs/several_clusters/jni/ThirdClient-InvokeSubscribeImpl.cpp
jni/CHIPCallbackTypes.h: outputs/several_clusters/jni/CHIPCallbackTypes.h
jni/CHIPReadCallbacks.h: outputs/several_clusters/jni/CHIPReadReadCallbacks.h
jni/CHIPGlobalCallbacks.cpp: outputs/shared/jni/CHIPGlobalCallbacks.cpp

inputs/optional_argument.matter:
jni/MyClusterClient-ReadImpl.cpp: outputs/optional_argument/jni/MyClusterClient-ReadImpl.cpp
jni/MyClusterClient-InvokeSubscribeImpl.cpp: outputs/optional_argument/jni/MyClusterClient-InvokeSubscribeImpl.cpp
jni/CHIPCallbackTypes.h: outputs/optional_argument/jni/CHIPCallbackTypes.h
jni/CHIPReadCallbacks.h: outputs/optional_argument/jni/CHIPReadReadCallbacks.h
jni/CHIPGlobalCallbacks.cpp: outputs/shared/jni/CHIPGlobalCallbacks.cpp

java-class:
inputs/several_clusters.matter:
Expand Down
Loading