From 43a9dc91b39312959f44ef1f82ffc3e20ad29c92 Mon Sep 17 00:00:00 2001 From: Joonhaeng Heo Date: Fri, 23 Feb 2024 18:23:09 +0900 Subject: [PATCH 1/4] Support General value parsing --- src/controller/java/AndroidCallbacks.cpp | 78 ++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/controller/java/AndroidCallbacks.cpp b/src/controller/java/AndroidCallbacks.cpp index 01ef820f82c6ed..a6498ff4cde2f3 100644 --- a/src/controller/java/AndroidCallbacks.cpp +++ b/src/controller/java/AndroidCallbacks.cpp @@ -41,6 +41,8 @@ static const int MILLIS_SINCE_EPOCH = 1; // Add the bytes for attribute tag(1:control + 8:tag + 8:length) and structure(1:struct + 1:close container) static const int EXTRA_SPACE_FOR_ATTRIBUTE_TAG = 19; +jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader * apData); + GetConnectedDeviceCallback::GetConnectedDeviceCallback(jobject wrapperCallback, jobject javaCallback, const char * callbackClassSignature) : mOnSuccess(OnDeviceConnectedFn, this), @@ -322,12 +324,15 @@ void ReportCallback::OnAttributeData(const app::ConcreteDataAttributePath & aPat // If we don't know this attribute, suppress it. if (err == CHIP_ERROR_IM_MALFORMED_ATTRIBUTE_PATH_IB) { + value = DecodeGeneralTLVValue(env, apData); err = CHIP_NO_ERROR; } VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Fail to decode attribute with error %s", ErrorStr(err)); aPath.LogPath()); VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe()); +#else + value = DecodeGeneralTLVValue(env, apData); #endif // Create TLV byte array to pass to Java layer size_t bufferLen = readerForJavaTLV.GetRemainingLength() + readerForJavaTLV.GetLengthRead(); @@ -475,11 +480,14 @@ void ReportCallback::OnEventData(const app::EventHeader & aEventHeader, TLV::TLV // If we don't know this event, just skip it. if (err == CHIP_ERROR_IM_MALFORMED_EVENT_PATH_IB) { + value = DecodeGeneralTLVValue(env, apData); err = CHIP_NO_ERROR; } VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Fail to decode event with error %s", ErrorStr(err)); aEventHeader.LogPath()); VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe()); +#else + value = DecodeGeneralTLVValue(env, apData); #endif // Create TLV byte array to pass to Java layer @@ -907,6 +915,76 @@ void InvokeCallback::ReportError(const char * message, ChipError::StorageType er VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe()); } +jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader * apData) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + jobject retValue = nullptr; + + TLV::TLVReader readerForJavaObject; + readerForJavaObject.Init(*apData); + + switch (readerForJavaObject.GetType()) + { + case TLV::kTLVType_SignedInteger: + { + int64_t signedValue; + VerifyOrReturnValue(readerForJavaObject.Get(signedValue) == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); + err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Long", "(J)V", static_cast(signedValue), retValue); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Create Boxed Object fail!")); + return retValue; + } + case TLV::kTLVType_UnsignedInteger: + { + uint64_t unsignedValue; + VerifyOrReturnValue(readerForJavaObject.Get(unsignedValue) == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); + err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Long", "(J)V", static_cast(unsignedValue), retValue); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Create Boxed Object fail!")); + return retValue; + } + case TLV::kTLVType_Boolean: + { + bool booleanValue; + VerifyOrReturnValue(readerForJavaObject.Get(booleanValue) == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); + err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Boolean", "(Z)V", static_cast(booleanValue), retValue); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Create Boxed Object fail!")); + return retValue; + } + case TLV::kTLVType_FloatingPointNumber: + { + double doubleValue; + VerifyOrReturnValue(readerForJavaObject.Get(doubleValue) == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); + err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Double", "(D)V", static_cast(doubleValue), retValue); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Create Boxed Object fail!")); + return retValue; + } + case TLV::kTLVType_UTF8String: + { + uint32_t bufferLen = readerForJavaObject.GetLength(); + std::unique_ptr buffer = std::unique_ptr(new char[bufferLen + 1]); + err = readerForJavaObject.GetString(buffer.get(), bufferLen + 1); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); + chip::CharSpan valueSpan(buffer.get(), bufferLen); + chip::JniReferences::GetInstance().CharToStringUTF(valueSpan, retValue); + return retValue; + } + case TLV::kTLVType_ByteString: + { + uint32_t bufferLen = readerForJavaObject.GetLength(); + std::unique_ptr buffer = std::unique_ptr(new uint8_t[bufferLen + 1]); + err = readerForJavaObject.GetBytes(buffer.get(), bufferLen + 1); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); + + jbyteArray valueByteArray = env->NewByteArray(static_cast(bufferLen)); + env->SetByteArrayRegion(valueByteArray, 0, static_cast(bufferLen), + reinterpret_cast(buffer.get())); + + return static_cast(valueByteArray); + } + default: + return nullptr; + } +} + jlong newConnectedDeviceCallback(JNIEnv * env, jobject self, jobject callback) { chip::DeviceLayer::StackLock lock; From c07e32051770b4a688d65cd8c2511cf3ed2f3b8c Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 23 Feb 2024 09:25:55 +0000 Subject: [PATCH 2/4] Restyled by clang-format --- src/controller/java/AndroidCallbacks.cpp | 117 ++++++++++++----------- 1 file changed, 59 insertions(+), 58 deletions(-) diff --git a/src/controller/java/AndroidCallbacks.cpp b/src/controller/java/AndroidCallbacks.cpp index a6498ff4cde2f3..a4a130073b0243 100644 --- a/src/controller/java/AndroidCallbacks.cpp +++ b/src/controller/java/AndroidCallbacks.cpp @@ -325,7 +325,7 @@ void ReportCallback::OnAttributeData(const app::ConcreteDataAttributePath & aPat if (err == CHIP_ERROR_IM_MALFORMED_ATTRIBUTE_PATH_IB) { value = DecodeGeneralTLVValue(env, apData); - err = CHIP_NO_ERROR; + err = CHIP_NO_ERROR; } VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Fail to decode attribute with error %s", ErrorStr(err)); @@ -481,7 +481,7 @@ void ReportCallback::OnEventData(const app::EventHeader & aEventHeader, TLV::TLV if (err == CHIP_ERROR_IM_MALFORMED_EVENT_PATH_IB) { value = DecodeGeneralTLVValue(env, apData); - err = CHIP_NO_ERROR; + err = CHIP_NO_ERROR; } VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Fail to decode event with error %s", ErrorStr(err)); aEventHeader.LogPath()); @@ -917,7 +917,7 @@ void InvokeCallback::ReportError(const char * message, ChipError::StorageType er jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader * apData) { - CHIP_ERROR err = CHIP_NO_ERROR; + CHIP_ERROR err = CHIP_NO_ERROR; jobject retValue = nullptr; TLV::TLVReader readerForJavaObject; @@ -925,63 +925,64 @@ jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader * apData) switch (readerForJavaObject.GetType()) { - case TLV::kTLVType_SignedInteger: - { - int64_t signedValue; - VerifyOrReturnValue(readerForJavaObject.Get(signedValue) == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); - err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Long", "(J)V", static_cast(signedValue), retValue); - VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Create Boxed Object fail!")); - return retValue; - } - case TLV::kTLVType_UnsignedInteger: - { - uint64_t unsignedValue; - VerifyOrReturnValue(readerForJavaObject.Get(unsignedValue) == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); - err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Long", "(J)V", static_cast(unsignedValue), retValue); - VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Create Boxed Object fail!")); - return retValue; - } - case TLV::kTLVType_Boolean: - { - bool booleanValue; - VerifyOrReturnValue(readerForJavaObject.Get(booleanValue) == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); - err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Boolean", "(Z)V", static_cast(booleanValue), retValue); - VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Create Boxed Object fail!")); - return retValue; - } - case TLV::kTLVType_FloatingPointNumber: - { - double doubleValue; - VerifyOrReturnValue(readerForJavaObject.Get(doubleValue) == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); - err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Double", "(D)V", static_cast(doubleValue), retValue); - VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Create Boxed Object fail!")); - return retValue; - } - case TLV::kTLVType_UTF8String: - { - uint32_t bufferLen = readerForJavaObject.GetLength(); - std::unique_ptr buffer = std::unique_ptr(new char[bufferLen + 1]); - err = readerForJavaObject.GetString(buffer.get(), bufferLen + 1); - VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); - chip::CharSpan valueSpan(buffer.get(), bufferLen); - chip::JniReferences::GetInstance().CharToStringUTF(valueSpan, retValue); - return retValue; - } - case TLV::kTLVType_ByteString: - { - uint32_t bufferLen = readerForJavaObject.GetLength(); - std::unique_ptr buffer = std::unique_ptr(new uint8_t[bufferLen + 1]); - err = readerForJavaObject.GetBytes(buffer.get(), bufferLen + 1); - VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); + case TLV::kTLVType_SignedInteger: { + int64_t signedValue; + VerifyOrReturnValue(readerForJavaObject.Get(signedValue) == CHIP_NO_ERROR, nullptr, + ChipLogProgress(Controller, "Get TLV Value fail!")); + err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Long", "(J)V", static_cast(signedValue), + retValue); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Create Boxed Object fail!")); + return retValue; + } + case TLV::kTLVType_UnsignedInteger: { + uint64_t unsignedValue; + VerifyOrReturnValue(readerForJavaObject.Get(unsignedValue) == CHIP_NO_ERROR, nullptr, + ChipLogProgress(Controller, "Get TLV Value fail!")); + err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Long", "(J)V", static_cast(unsignedValue), + retValue); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Create Boxed Object fail!")); + return retValue; + } + case TLV::kTLVType_Boolean: { + bool booleanValue; + VerifyOrReturnValue(readerForJavaObject.Get(booleanValue) == CHIP_NO_ERROR, nullptr, + ChipLogProgress(Controller, "Get TLV Value fail!")); + err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Boolean", "(Z)V", + static_cast(booleanValue), retValue); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Create Boxed Object fail!")); + return retValue; + } + case TLV::kTLVType_FloatingPointNumber: { + double doubleValue; + VerifyOrReturnValue(readerForJavaObject.Get(doubleValue) == CHIP_NO_ERROR, nullptr, + ChipLogProgress(Controller, "Get TLV Value fail!")); + err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Double", "(D)V", static_cast(doubleValue), + retValue); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Create Boxed Object fail!")); + return retValue; + } + case TLV::kTLVType_UTF8String: { + uint32_t bufferLen = readerForJavaObject.GetLength(); + std::unique_ptr buffer = std::unique_ptr(new char[bufferLen + 1]); + err = readerForJavaObject.GetString(buffer.get(), bufferLen + 1); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); + chip::CharSpan valueSpan(buffer.get(), bufferLen); + chip::JniReferences::GetInstance().CharToStringUTF(valueSpan, retValue); + return retValue; + } + case TLV::kTLVType_ByteString: { + uint32_t bufferLen = readerForJavaObject.GetLength(); + std::unique_ptr buffer = std::unique_ptr(new uint8_t[bufferLen + 1]); + err = readerForJavaObject.GetBytes(buffer.get(), bufferLen + 1); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); - jbyteArray valueByteArray = env->NewByteArray(static_cast(bufferLen)); - env->SetByteArrayRegion(valueByteArray, 0, static_cast(bufferLen), - reinterpret_cast(buffer.get())); + jbyteArray valueByteArray = env->NewByteArray(static_cast(bufferLen)); + env->SetByteArrayRegion(valueByteArray, 0, static_cast(bufferLen), reinterpret_cast(buffer.get())); - return static_cast(valueByteArray); - } - default: - return nullptr; + return static_cast(valueByteArray); + } + default: + return nullptr; } } From bdfb2aa38aa714ab406e9761e195465360c4e311 Mon Sep 17 00:00:00 2001 From: Joonhaeng Heo Date: Mon, 26 Feb 2024 16:42:06 +0900 Subject: [PATCH 3/4] Add List, Array TLV --- src/controller/java/AndroidCallbacks.cpp | 83 ++++++++++++++++-------- 1 file changed, 57 insertions(+), 26 deletions(-) diff --git a/src/controller/java/AndroidCallbacks.cpp b/src/controller/java/AndroidCallbacks.cpp index a4a130073b0243..fd052100344ba3 100644 --- a/src/controller/java/AndroidCallbacks.cpp +++ b/src/controller/java/AndroidCallbacks.cpp @@ -41,7 +41,7 @@ static const int MILLIS_SINCE_EPOCH = 1; // Add the bytes for attribute tag(1:control + 8:tag + 8:length) and structure(1:struct + 1:close container) static const int EXTRA_SPACE_FOR_ATTRIBUTE_TAG = 19; -jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader * apData); +jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader & readerForGeneralValueObject, CHIP_ERROR &err); GetConnectedDeviceCallback::GetConnectedDeviceCallback(jobject wrapperCallback, jobject javaCallback, const char * callbackClassSignature) : @@ -316,15 +316,16 @@ void ReportCallback::OnAttributeData(const app::ConcreteDataAttributePath & aPat readerForJavaTLV.Init(*apData); jobject value = nullptr; -#ifdef USE_JAVA_TLV_ENCODE_DECODE TLV::TLVReader readerForJavaObject; readerForJavaObject.Init(*apData); - +#ifdef USE_JAVA_TLV_ENCODE_DECODE value = DecodeAttributeValue(aPath, readerForJavaObject, &err); // If we don't know this attribute, suppress it. if (err == CHIP_ERROR_IM_MALFORMED_ATTRIBUTE_PATH_IB) { - value = DecodeGeneralTLVValue(env, apData); + TLV::TLVReader readerForGeneralValueObject; + readerForGeneralValueObject.Init(*apData); + value = DecodeGeneralTLVValue(env, readerForGeneralValueObject, err); err = CHIP_NO_ERROR; } @@ -332,7 +333,7 @@ void ReportCallback::OnAttributeData(const app::ConcreteDataAttributePath & aPat aPath.LogPath()); VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe()); #else - value = DecodeGeneralTLVValue(env, apData); + value = DecodeGeneralTLVValue(env, readerForJavaObject, err); #endif // Create TLV byte array to pass to Java layer size_t bufferLen = readerForJavaTLV.GetRemainingLength() + readerForJavaTLV.GetLengthRead(); @@ -473,21 +474,23 @@ void ReportCallback::OnEventData(const app::EventHeader & aEventHeader, TLV::TLV } jobject value = nullptr; -#ifdef USE_JAVA_TLV_ENCODE_DECODE TLV::TLVReader readerForJavaObject; readerForJavaObject.Init(*apData); +#ifdef USE_JAVA_TLV_ENCODE_DECODE value = DecodeEventValue(aEventHeader.mPath, readerForJavaObject, &err); // If we don't know this event, just skip it. if (err == CHIP_ERROR_IM_MALFORMED_EVENT_PATH_IB) { - value = DecodeGeneralTLVValue(env, apData); + TLV::TLVReader readerForGeneralValueObject; + readerForGeneralValueObject.Init(*apData); + value = DecodeGeneralTLVValue(env, readerForGeneralValueObject, err); err = CHIP_NO_ERROR; } VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Fail to decode event with error %s", ErrorStr(err)); aEventHeader.LogPath()); VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe()); #else - value = DecodeGeneralTLVValue(env, apData); + value = DecodeGeneralTLVValue(env, readerForJavaObject, err); #endif // Create TLV byte array to pass to Java layer @@ -915,19 +918,15 @@ void InvokeCallback::ReportError(const char * message, ChipError::StorageType er VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe()); } -jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader * apData) +jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader & readerForGeneralValueObject, CHIP_ERROR &err) { - CHIP_ERROR err = CHIP_NO_ERROR; jobject retValue = nullptr; - TLV::TLVReader readerForJavaObject; - readerForJavaObject.Init(*apData); - - switch (readerForJavaObject.GetType()) + switch (readerForGeneralValueObject.GetType()) { case TLV::kTLVType_SignedInteger: { - int64_t signedValue; - VerifyOrReturnValue(readerForJavaObject.Get(signedValue) == CHIP_NO_ERROR, nullptr, + int64_t signedValue = 0; + VerifyOrReturnValue(readerForGeneralValueObject.Get(signedValue) == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Long", "(J)V", static_cast(signedValue), retValue); @@ -935,8 +934,8 @@ jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader * apData) return retValue; } case TLV::kTLVType_UnsignedInteger: { - uint64_t unsignedValue; - VerifyOrReturnValue(readerForJavaObject.Get(unsignedValue) == CHIP_NO_ERROR, nullptr, + uint64_t unsignedValue = 0; + VerifyOrReturnValue(readerForGeneralValueObject.Get(unsignedValue) == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Long", "(J)V", static_cast(unsignedValue), retValue); @@ -944,8 +943,8 @@ jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader * apData) return retValue; } case TLV::kTLVType_Boolean: { - bool booleanValue; - VerifyOrReturnValue(readerForJavaObject.Get(booleanValue) == CHIP_NO_ERROR, nullptr, + bool booleanValue = false; + VerifyOrReturnValue(readerForGeneralValueObject.Get(booleanValue) == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Boolean", "(Z)V", static_cast(booleanValue), retValue); @@ -953,8 +952,8 @@ jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader * apData) return retValue; } case TLV::kTLVType_FloatingPointNumber: { - double doubleValue; - VerifyOrReturnValue(readerForJavaObject.Get(doubleValue) == CHIP_NO_ERROR, nullptr, + double doubleValue = 0.0; + VerifyOrReturnValue(readerForGeneralValueObject.Get(doubleValue) == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); err = JniReferences::GetInstance().CreateBoxedObject("java/lang/Double", "(D)V", static_cast(doubleValue), retValue); @@ -962,18 +961,18 @@ jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader * apData) return retValue; } case TLV::kTLVType_UTF8String: { - uint32_t bufferLen = readerForJavaObject.GetLength(); + uint32_t bufferLen = readerForGeneralValueObject.GetLength(); std::unique_ptr buffer = std::unique_ptr(new char[bufferLen + 1]); - err = readerForJavaObject.GetString(buffer.get(), bufferLen + 1); + err = readerForGeneralValueObject.GetString(buffer.get(), bufferLen + 1); VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); chip::CharSpan valueSpan(buffer.get(), bufferLen); chip::JniReferences::GetInstance().CharToStringUTF(valueSpan, retValue); return retValue; } case TLV::kTLVType_ByteString: { - uint32_t bufferLen = readerForJavaObject.GetLength(); + uint32_t bufferLen = readerForGeneralValueObject.GetLength(); std::unique_ptr buffer = std::unique_ptr(new uint8_t[bufferLen + 1]); - err = readerForJavaObject.GetBytes(buffer.get(), bufferLen + 1); + err = readerForGeneralValueObject.GetBytes(buffer.get(), bufferLen + 1); VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Get TLV Value fail!")); jbyteArray valueByteArray = env->NewByteArray(static_cast(bufferLen)); @@ -981,7 +980,39 @@ jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader * apData) return static_cast(valueByteArray); } + case TLV::kTLVType_Array: { + TLV::TLVType containerType; + err = readerForGeneralValueObject.EnterContainer(containerType); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "EnterContainer fail! : %" CHIP_ERROR_FORMAT, err.Format())); + err = chip::JniReferences::GetInstance().CreateArrayList(retValue); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "CreateArrayList fail! : %" CHIP_ERROR_FORMAT, err.Format())); + while(readerForGeneralValueObject.Next() == CHIP_NO_ERROR) + { + jobject inValue = DecodeGeneralTLVValue(env, readerForGeneralValueObject, err); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Can't parse general value : %" CHIP_ERROR_FORMAT, err.Format())); + err = chip::JniReferences::GetInstance().AddToList(retValue, inValue); + } + return retValue; + } + case TLV::kTLVType_List: { + TLV::TLVType containerType; + err = readerForGeneralValueObject.EnterContainer(containerType); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "EnterContainer fail! : %" CHIP_ERROR_FORMAT, err.Format())); + err = chip::JniReferences::GetInstance().CreateArrayList(retValue); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "CreateArrayList fail! : %" CHIP_ERROR_FORMAT, err.Format())); + while(readerForGeneralValueObject.Next() == CHIP_NO_ERROR) + { + jobject inValue = DecodeGeneralTLVValue(env, readerForGeneralValueObject, err); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Can't parse general value : %" CHIP_ERROR_FORMAT, err.Format())); + err = chip::JniReferences::GetInstance().AddToList(retValue, inValue); + } + return retValue; + } + case TLV::kTLVType_Null: { + return nullptr; + } default: + err = CHIP_ERROR_WRONG_TLV_TYPE; return nullptr; } } From 36be4bd46fd98bdcb588f9e59d185307ab672501 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Mon, 26 Feb 2024 07:42:28 +0000 Subject: [PATCH 4/4] Restyled by clang-format --- src/controller/java/AndroidCallbacks.cpp | 26 +++++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/controller/java/AndroidCallbacks.cpp b/src/controller/java/AndroidCallbacks.cpp index fd052100344ba3..fb7975262dff57 100644 --- a/src/controller/java/AndroidCallbacks.cpp +++ b/src/controller/java/AndroidCallbacks.cpp @@ -41,7 +41,7 @@ static const int MILLIS_SINCE_EPOCH = 1; // Add the bytes for attribute tag(1:control + 8:tag + 8:length) and structure(1:struct + 1:close container) static const int EXTRA_SPACE_FOR_ATTRIBUTE_TAG = 19; -jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader & readerForGeneralValueObject, CHIP_ERROR &err); +jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader & readerForGeneralValueObject, CHIP_ERROR & err); GetConnectedDeviceCallback::GetConnectedDeviceCallback(jobject wrapperCallback, jobject javaCallback, const char * callbackClassSignature) : @@ -918,7 +918,7 @@ void InvokeCallback::ReportError(const char * message, ChipError::StorageType er VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe()); } -jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader & readerForGeneralValueObject, CHIP_ERROR &err) +jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader & readerForGeneralValueObject, CHIP_ERROR & err) { jobject retValue = nullptr; @@ -983,13 +983,16 @@ jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader & readerForGeneralVal case TLV::kTLVType_Array: { TLV::TLVType containerType; err = readerForGeneralValueObject.EnterContainer(containerType); - VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "EnterContainer fail! : %" CHIP_ERROR_FORMAT, err.Format())); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, + ChipLogProgress(Controller, "EnterContainer fail! : %" CHIP_ERROR_FORMAT, err.Format())); err = chip::JniReferences::GetInstance().CreateArrayList(retValue); - VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "CreateArrayList fail! : %" CHIP_ERROR_FORMAT, err.Format())); - while(readerForGeneralValueObject.Next() == CHIP_NO_ERROR) + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, + ChipLogProgress(Controller, "CreateArrayList fail! : %" CHIP_ERROR_FORMAT, err.Format())); + while (readerForGeneralValueObject.Next() == CHIP_NO_ERROR) { jobject inValue = DecodeGeneralTLVValue(env, readerForGeneralValueObject, err); - VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Can't parse general value : %" CHIP_ERROR_FORMAT, err.Format())); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, + ChipLogProgress(Controller, "Can't parse general value : %" CHIP_ERROR_FORMAT, err.Format())); err = chip::JniReferences::GetInstance().AddToList(retValue, inValue); } return retValue; @@ -997,13 +1000,16 @@ jobject DecodeGeneralTLVValue(JNIEnv * env, TLV::TLVReader & readerForGeneralVal case TLV::kTLVType_List: { TLV::TLVType containerType; err = readerForGeneralValueObject.EnterContainer(containerType); - VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "EnterContainer fail! : %" CHIP_ERROR_FORMAT, err.Format())); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, + ChipLogProgress(Controller, "EnterContainer fail! : %" CHIP_ERROR_FORMAT, err.Format())); err = chip::JniReferences::GetInstance().CreateArrayList(retValue); - VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "CreateArrayList fail! : %" CHIP_ERROR_FORMAT, err.Format())); - while(readerForGeneralValueObject.Next() == CHIP_NO_ERROR) + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, + ChipLogProgress(Controller, "CreateArrayList fail! : %" CHIP_ERROR_FORMAT, err.Format())); + while (readerForGeneralValueObject.Next() == CHIP_NO_ERROR) { jobject inValue = DecodeGeneralTLVValue(env, readerForGeneralValueObject, err); - VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, ChipLogProgress(Controller, "Can't parse general value : %" CHIP_ERROR_FORMAT, err.Format())); + VerifyOrReturnValue(err == CHIP_NO_ERROR, nullptr, + ChipLogProgress(Controller, "Can't parse general value : %" CHIP_ERROR_FORMAT, err.Format())); err = chip::JniReferences::GetInstance().AddToList(retValue, inValue); } return retValue;