From 2e53fdf88ed46c3deac9f96dfbe35db8315abe17 Mon Sep 17 00:00:00 2001 From: Sharad Binjola <31142146+sharadb-amazon@users.noreply.github.com> Date: Thu, 2 Feb 2023 17:30:52 -0800 Subject: [PATCH] tv-casting-app: Fixing issues passing uniqueIDs and Video player caching (#24745) * tv-casting-app/android: Updates to how the rotatingDeviceIdUniqueId is passed * tv-casting-app: Fixing persistence logic --- .../jni/com/chip/casting/AppParameters.java | 18 ++++++++-- .../app/src/main/jni/cpp/ConversionUtils.cpp | 23 ++++++++++--- .../include/CHIPProjectAppConfig.h | 2 +- .../src/PersistenceManager.cpp | 33 ++++++++++++------- 4 files changed, 57 insertions(+), 19 deletions(-) diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/AppParameters.java b/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/AppParameters.java index 78cbad859fc961..cf4d626bac248c 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/AppParameters.java +++ b/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/AppParameters.java @@ -17,7 +17,12 @@ */ package com.chip.casting; +import android.util.Log; +import java.math.BigInteger; +import java.util.Arrays; + public class AppParameters { + private static final String TAG = AppParameters.class.getSimpleName(); public static final int MIN_ROTATING_DEVICE_ID_UNIQUE_ID_LENGTH = 16; private static final int TEST_SETUP_PASSCODE = 20202021; private static final int TEST_DISCRIMINATOR = 0xF00; @@ -32,11 +37,20 @@ public class AppParameters { private int discriminator = TEST_DISCRIMINATOR; public void setRotatingDeviceIdUniqueId(byte[] rotatingDeviceIdUniqueId) { - this.rotatingDeviceIdUniqueId = rotatingDeviceIdUniqueId; + Log.d( + TAG, + "AppParameters.setRotatingDeviceIdUniqueId called with " + + new BigInteger(1, rotatingDeviceIdUniqueId).toString(16)); + this.rotatingDeviceIdUniqueId = + Arrays.copyOf(rotatingDeviceIdUniqueId, rotatingDeviceIdUniqueId.length); } public byte[] getRotatingDeviceIdUniqueId() { - return rotatingDeviceIdUniqueId; + Log.d( + TAG, + "AppParameters.getRotatingDeviceIdUniqueId returning copyOf " + + new BigInteger(1, rotatingDeviceIdUniqueId).toString(16)); + return Arrays.copyOf(rotatingDeviceIdUniqueId, rotatingDeviceIdUniqueId.length); } public DACProvider getDacProvider() { diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/ConversionUtils.cpp b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/ConversionUtils.cpp index 8112c6b2acfb90..cff8ddd8ba9677 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/ConversionUtils.cpp +++ b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/ConversionUtils.cpp @@ -32,12 +32,27 @@ CHIP_ERROR convertJAppParametersToCppAppParams(jobject appParameters, AppParams ReturnErrorOnFailure( chip::JniReferences::GetInstance().GetClassRef(env, "com/chip/casting/AppParameters", jAppParametersClass)); - jfieldID jRotatingDeviceIdUniqueIdField = env->GetFieldID(jAppParametersClass, "rotatingDeviceIdUniqueId", "[B"); - jobject jRotatingDeviceIdUniqueId = env->GetObjectField(appParameters, jRotatingDeviceIdUniqueIdField); + jmethodID getRotatingDeviceIdUniqueIdMethod = env->GetMethodID(jAppParametersClass, "getRotatingDeviceIdUniqueId", "()[B"); + if (getRotatingDeviceIdUniqueIdMethod == nullptr) + { + ChipLogError(Zcl, "Failed to access AppParameters 'getRotatingDeviceIdUniqueId' method"); + env->ExceptionClear(); + } + + jobject jRotatingDeviceIdUniqueId = (jobject) env->CallObjectMethod(appParameters, getRotatingDeviceIdUniqueIdMethod); + if (env->ExceptionCheck()) + { + ChipLogError(Zcl, "Java exception in AppParameters::getRotatingDeviceIdUniqueId"); + env->ExceptionDescribe(); + env->ExceptionClear(); + return CHIP_ERROR_INCORRECT_STATE; + } + if (jRotatingDeviceIdUniqueId != nullptr) { - chip::JniByteArray jniRotatingDeviceIdUniqueIdByteArray(env, static_cast(jRotatingDeviceIdUniqueId)); - outAppParams.SetRotatingDeviceIdUniqueId(MakeOptional(jniRotatingDeviceIdUniqueIdByteArray.byteSpan())); + chip::JniByteArray * jniRotatingDeviceIdUniqueIdByteArray = + new chip::JniByteArray(env, static_cast(jRotatingDeviceIdUniqueId)); + outAppParams.SetRotatingDeviceIdUniqueId(MakeOptional(jniRotatingDeviceIdUniqueIdByteArray->byteSpan())); } return CHIP_NO_ERROR; diff --git a/examples/tv-casting-app/tv-casting-common/include/CHIPProjectAppConfig.h b/examples/tv-casting-app/tv-casting-common/include/CHIPProjectAppConfig.h index e11b96824bcd5a..641cf0d120be0c 100644 --- a/examples/tv-casting-app/tv-casting-common/include/CHIPProjectAppConfig.h +++ b/examples/tv-casting-app/tv-casting-common/include/CHIPProjectAppConfig.h @@ -60,4 +60,4 @@ #define CHIP_ENABLE_ROTATING_DEVICE_ID 1 -#define CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID_LENGTH 64 +#define CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID_LENGTH 128 diff --git a/examples/tv-casting-app/tv-casting-common/src/PersistenceManager.cpp b/examples/tv-casting-app/tv-casting-common/src/PersistenceManager.cpp index 1e92e6eb4e2e13..dea0277064b30c 100644 --- a/examples/tv-casting-app/tv-casting-common/src/PersistenceManager.cpp +++ b/examples/tv-casting-app/tv-casting-common/src/PersistenceManager.cpp @@ -46,12 +46,17 @@ CHIP_ERROR PersistenceManager::AddVideoPlayer(TargetVideoPlayerInfo * targetVide // found the same video player, overwrite the data if (cachedVideoPlayers[i] == *targetVideoPlayerInfo) { + ChipLogProgress(AppServer, + "PersistenceManager::AddVideoPlayer found video player already cached. Overwriting at position: %lu", + static_cast(i)); cachedVideoPlayers[i] = *targetVideoPlayerInfo; newVideoPlayer = false; } } if (newVideoPlayer) { + ChipLogProgress(AppServer, "PersistenceManager::AddVideoPlayer writing new video player at position: %lu", + static_cast(i)); VerifyOrReturnError(i < kMaxCachedVideoPlayers, CHIP_ERROR_BUFFER_TOO_SMALL); cachedVideoPlayers[i] = *targetVideoPlayerInfo; } @@ -78,7 +83,8 @@ CHIP_ERROR PersistenceManager::WriteAllVideoPlayers(TargetVideoPlayerInfo videoP ReturnErrorOnFailure( tlvWriter.StartContainer(TLV::ContextTag(kVideoPlayersContainerTag), TLV::kTLVType_Structure, videoPlayersContainerType)); - for (size_t videoPlayerIndex = 0; videoPlayerIndex < kMaxCachedVideoPlayers && videoPlayers[videoPlayerIndex].IsInitialized(); + size_t videoPlayerIndex; + for (videoPlayerIndex = 0; videoPlayerIndex < kMaxCachedVideoPlayers && videoPlayers[videoPlayerIndex].IsInitialized(); videoPlayerIndex++) { TargetVideoPlayerInfo * videoPlayer = &videoPlayers[videoPlayerIndex]; @@ -146,19 +152,19 @@ CHIP_ERROR PersistenceManager::WriteAllVideoPlayers(TargetVideoPlayerInfo videoP // Content app endpoints container ends ReturnErrorOnFailure(tlvWriter.EndContainer(contentAppEndpointsContainerType)); } - // Video Players container ends - ReturnErrorOnFailure(tlvWriter.EndContainer(videoPlayersContainerType)); - ReturnErrorOnFailure(tlvWriter.EndContainer(outerContainerType)); - - ReturnErrorOnFailure(tlvWriter.Finalize()); - ChipLogProgress(AppServer, - "PersistenceManager::WriteAllVideoPlayers TLV(CastingData).LengthWritten: %d bytes and version: %d", - tlvWriter.GetLengthWritten(), kCurrentCastingDataVersion); - return chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Put(kCastingDataKey, castingData, - tlvWriter.GetLengthWritten()); } } - return CHIP_NO_ERROR; + + // Video Players container ends + ReturnErrorOnFailure(tlvWriter.EndContainer(videoPlayersContainerType)); + ReturnErrorOnFailure(tlvWriter.EndContainer(outerContainerType)); + + ReturnErrorOnFailure(tlvWriter.Finalize()); + ChipLogProgress(AppServer, + "PersistenceManager::WriteAllVideoPlayers TLV(CastingData).LengthWritten: %d bytes, video player count: %lu " + "and version: %d", + tlvWriter.GetLengthWritten(), static_cast(videoPlayerIndex), kCurrentCastingDataVersion); + return chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Put(kCastingDataKey, castingData, tlvWriter.GetLengthWritten()); } CHIP_ERROR PersistenceManager::ReadAllVideoPlayers(TargetVideoPlayerInfo outVideoPlayers[]) @@ -379,6 +385,9 @@ CHIP_ERROR PersistenceManager::ReadAllVideoPlayers(TargetVideoPlayerInfo outVide VerifyOrReturnError(err == CHIP_END_OF_TLV, err); ReturnErrorOnFailure(reader.ExitContainer(videoPlayersContainerType)); ReturnErrorOnFailure(reader.ExitContainer(outerContainerType)); + + ChipLogProgress(AppServer, "PersistenceManager::ReadAllVideoPlayers Video player read count: %lu", + static_cast(videoPlayerIndex)); return CHIP_NO_ERROR; }