Skip to content

Commit

Permalink
tv-casting-app: Fixing issues passing uniqueIDs and Video player cach…
Browse files Browse the repository at this point in the history
…ing (#24745)

* tv-casting-app/android: Updates to how the rotatingDeviceIdUniqueId is passed

* tv-casting-app: Fixing persistence logic
  • Loading branch information
sharadb-amazon authored and pull[bot] committed Mar 5, 2024
1 parent ea8f195 commit 2e53fdf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<jbyteArray>(jRotatingDeviceIdUniqueId));
outAppParams.SetRotatingDeviceIdUniqueId(MakeOptional(jniRotatingDeviceIdUniqueIdByteArray.byteSpan()));
chip::JniByteArray * jniRotatingDeviceIdUniqueIdByteArray =
new chip::JniByteArray(env, static_cast<jbyteArray>(jRotatingDeviceIdUniqueId));
outAppParams.SetRotatingDeviceIdUniqueId(MakeOptional(jniRotatingDeviceIdUniqueIdByteArray->byteSpan()));
}

return CHIP_NO_ERROR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned long>(i));
cachedVideoPlayers[i] = *targetVideoPlayerInfo;
newVideoPlayer = false;
}
}
if (newVideoPlayer)
{
ChipLogProgress(AppServer, "PersistenceManager::AddVideoPlayer writing new video player at position: %lu",
static_cast<unsigned long>(i));
VerifyOrReturnError(i < kMaxCachedVideoPlayers, CHIP_ERROR_BUFFER_TOO_SMALL);
cachedVideoPlayers[i] = *targetVideoPlayerInfo;
}
Expand All @@ -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];
Expand Down Expand Up @@ -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<unsigned long>(videoPlayerIndex), kCurrentCastingDataVersion);
return chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr().Put(kCastingDataKey, castingData, tlvWriter.GetLengthWritten());
}

CHIP_ERROR PersistenceManager::ReadAllVideoPlayers(TargetVideoPlayerInfo outVideoPlayers[])
Expand Down Expand Up @@ -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<unsigned long>(videoPlayerIndex));
return CHIP_NO_ERROR;
}

Expand Down

0 comments on commit 2e53fdf

Please sign in to comment.