Skip to content

Commit f2cd15f

Browse files
[Android] Integrate cluster state cache with android Java IM API (#22851)
* Integrate cluster state cache with java im API * address comments
1 parent c2e0f70 commit f2cd15f

8 files changed

+82
-14
lines changed

src/app/ClusterStateCache.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -532,5 +532,10 @@ CHIP_ERROR ClusterStateCache::OnUpdateDataVersionFilterList(DataVersionFilterIBs
532532
return err;
533533
}
534534

535+
CHIP_ERROR ClusterStateCache::GetLastReportDataPath(ConcreteClusterPath & aPath)
536+
{
537+
aPath = mLastReportDataPath;
538+
return CHIP_NO_ERROR;
539+
}
535540
} // namespace app
536541
} // namespace chip

src/app/ClusterStateCache.h

+2
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ class ClusterStateCache : protected ReadClient::Callback
483483
mEventStatusCache.clear();
484484
}
485485

486+
CHIP_ERROR GetLastReportDataPath(ConcreteClusterPath & aPath);
487+
486488
private:
487489
using AttributeState = Variant<Platform::ScopedMemoryBufferWithSize<uint8_t>, StatusIB>;
488490
// mPendingDataVersion represents a tentative data version for a cluster that we have gotten some reports for.

src/controller/java/AndroidCallbacks.cpp

+42-4
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void GetConnectedDeviceCallback::OnDeviceConnectionFailureFn(void * context, con
121121

122122
ReportCallback::ReportCallback(jobject wrapperCallback, jobject subscriptionEstablishedCallback, jobject reportCallback,
123123
jobject resubscriptionAttemptCallback) :
124-
mBufferedReadAdapter(*this)
124+
mClusterCacheAdapter(*this)
125125
{
126126
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
127127
VerifyOrReturn(env != nullptr, ChipLogError(Controller, "Could not get JNIEnv for current thread"));
@@ -186,6 +186,8 @@ void ReportCallback::OnReportBegin()
186186

187187
void ReportCallback::OnReportEnd()
188188
{
189+
UpdateClusterDataVersion();
190+
189191
// Transform C++ jobject pair list to a Java HashMap, and call onReport() on the Java callback.
190192
CHIP_ERROR err = CHIP_NO_ERROR;
191193
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
@@ -280,7 +282,43 @@ void ReportCallback::OnAttributeData(const app::ConcreteDataAttributePath & aPat
280282
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find addAttribute method"));
281283
env->CallVoidMethod(mNodeStateObj, addAttributeMethod, static_cast<jint>(aPath.mEndpointId),
282284
static_cast<jlong>(aPath.mClusterId), static_cast<jlong>(aPath.mAttributeId), attributeStateObj);
283-
VerifyOrReturnError(!env->ExceptionCheck(), env->ExceptionDescribe());
285+
VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe());
286+
287+
UpdateClusterDataVersion();
288+
}
289+
290+
void ReportCallback::UpdateClusterDataVersion()
291+
{
292+
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
293+
chip::app::ConcreteClusterPath lastConcreteClusterPath;
294+
295+
if (mClusterCacheAdapter.GetLastReportDataPath(lastConcreteClusterPath) != CHIP_NO_ERROR)
296+
{
297+
return;
298+
}
299+
300+
if (!lastConcreteClusterPath.IsValidConcreteClusterPath())
301+
{
302+
return;
303+
}
304+
305+
chip::Optional<chip::DataVersion> committedDataVersion;
306+
if (mClusterCacheAdapter.GetVersion(lastConcreteClusterPath, committedDataVersion) != CHIP_NO_ERROR)
307+
{
308+
return;
309+
}
310+
if (!committedDataVersion.HasValue())
311+
{
312+
return;
313+
}
314+
315+
// SetDataVersion to NodeState
316+
jmethodID setDataVersionMethod;
317+
CHIP_ERROR err = JniReferences::GetInstance().FindMethod(env, mNodeStateObj, "setDataVersion", "(IJI)V", &setDataVersionMethod);
318+
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find setDataVersion method"));
319+
env->CallVoidMethod(mNodeStateObj, setDataVersionMethod, static_cast<jint>(lastConcreteClusterPath.mEndpointId),
320+
static_cast<jlong>(lastConcreteClusterPath.mClusterId), static_cast<jint>(committedDataVersion.Value()));
321+
VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe());
284322
}
285323

286324
void ReportCallback::OnEventData(const app::EventHeader & aEventHeader, TLV::TLVReader * apData, const app::StatusIB * apStatus)
@@ -356,7 +394,7 @@ void ReportCallback::OnEventData(const app::EventHeader & aEventHeader, TLV::TLV
356394
env->CallVoidMethod(mNodeStateObj, addEventMethod, static_cast<jint>(aEventHeader.mPath.mEndpointId),
357395
static_cast<jlong>(aEventHeader.mPath.mClusterId), static_cast<jlong>(aEventHeader.mPath.mEventId),
358396
eventStateObj);
359-
VerifyOrReturnError(!env->ExceptionCheck(), env->ExceptionDescribe());
397+
VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe());
360398
}
361399

362400
CHIP_ERROR ReportCallback::CreateChipAttributePath(const app::ConcreteDataAttributePath & aPath, jobject & outObj)
@@ -628,7 +666,7 @@ void ReportEventCallback::OnEventData(const app::EventHeader & aEventHeader, TLV
628666
env->CallVoidMethod(mNodeStateObj, addEventMethod, static_cast<jint>(aEventHeader.mPath.mEndpointId),
629667
static_cast<jlong>(aEventHeader.mPath.mClusterId), static_cast<jlong>(aEventHeader.mPath.mEventId),
630668
eventStateObj);
631-
VerifyOrReturnError(!env->ExceptionCheck(), env->ExceptionDescribe());
669+
VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe());
632670
}
633671

634672
CHIP_ERROR ReportEventCallback::CreateChipEventPath(const app::ConcreteEventPath & aPath, jobject & outObj)

src/controller/java/AndroidCallbacks.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct GetConnectedDeviceCallback
4242
jobject mJavaCallbackRef = nullptr;
4343
};
4444

45-
struct ReportCallback : public app::ReadClient::Callback
45+
struct ReportCallback : public app::ClusterStateCache::Callback
4646
{
4747
/** Subscription established callback can be nullptr. */
4848
ReportCallback(jobject wrapperCallback, jobject subscriptionEstablishedCallback, jobject reportCallback,
@@ -74,9 +74,11 @@ struct ReportCallback : public app::ReadClient::Callback
7474

7575
CHIP_ERROR CreateChipEventPath(const app::ConcreteEventPath & aPath, jobject & outObj);
7676

77+
void UpdateClusterDataVersion();
78+
7779
app::ReadClient * mReadClient = nullptr;
7880

79-
app::BufferedReadCallback mBufferedReadAdapter;
81+
app::ClusterStateCache mClusterCacheAdapter;
8082
jobject mWrapperCallbackRef = nullptr;
8183
jobject mSubscriptionEstablishedCallbackRef = nullptr;
8284
jobject mResubscriptionAttemptCallbackRef = nullptr;

src/controller/java/CHIPDeviceController-JNI.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -1077,9 +1077,9 @@ JNI_METHOD(void, subscribe)
10771077

10781078
auto callback = reinterpret_cast<ReportCallback *>(callbackHandle);
10791079

1080-
app::ReadClient * readClient =
1081-
Platform::New<app::ReadClient>(app::InteractionModelEngine::GetInstance(), device->GetExchangeManager(),
1082-
callback->mBufferedReadAdapter, app::ReadClient::InteractionType::Subscribe);
1080+
app::ReadClient * readClient = Platform::New<app::ReadClient>(
1081+
app::InteractionModelEngine::GetInstance(), device->GetExchangeManager(),
1082+
callback->mClusterCacheAdapter.GetBufferedCallback(), app::ReadClient::InteractionType::Subscribe);
10831083

10841084
err = readClient->SendRequest(params);
10851085
if (err != CHIP_NO_ERROR)
@@ -1126,9 +1126,9 @@ JNI_METHOD(void, read)
11261126

11271127
auto callback = reinterpret_cast<ReportCallback *>(callbackHandle);
11281128

1129-
app::ReadClient * readClient =
1130-
Platform::New<app::ReadClient>(app::InteractionModelEngine::GetInstance(), device->GetExchangeManager(),
1131-
callback->mBufferedReadAdapter, app::ReadClient::InteractionType::Read);
1129+
app::ReadClient * readClient = Platform::New<app::ReadClient>(
1130+
app::InteractionModelEngine::GetInstance(), device->GetExchangeManager(),
1131+
callback->mClusterCacheAdapter.GetBufferedCallback(), app::ReadClient::InteractionType::Read);
11321132

11331133
err = readClient->SendRequest(params);
11341134
if (err != CHIP_NO_ERROR)

src/controller/java/src/chip/devicecontroller/ChipDeviceController.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ public byte[] getAttestationChallenge(long devicePtr) {
452452
}
453453

454454
/** Subscribe to the given attribute path. */
455-
public void subscribeToPath(
455+
public void subscribeToAttributePath(
456456
SubscriptionEstablishedCallback subscriptionEstablishedCallback,
457457
ReportCallback reportCallback,
458458
long devicePtr,
@@ -524,7 +524,7 @@ public void subscribeToPath(
524524
}
525525

526526
/** Read the given attribute path. */
527-
public void readPath(
527+
public void readAttributePath(
528528
ReportCallback callback, long devicePtr, List<ChipAttributePath> attributePaths) {
529529
ReportCallbackJni jniCallback = new ReportCallbackJni(null, callback, null);
530530
read(

src/controller/java/src/chip/devicecontroller/model/ClusterState.java

+11
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@
1919

2020
import androidx.annotation.Nullable;
2121
import java.util.Map;
22+
import java.util.Optional;
2223

2324
/** Class for tracking CHIP cluster state in a hierarchical manner. */
2425
public final class ClusterState {
2526
private Map<Long, AttributeState> attributes;
2627
private Map<Long, EventState> events;
28+
private Optional<Integer> dataVersion;
2729

2830
public ClusterState(Map<Long, AttributeState> attributes, Map<Long, EventState> events) {
2931
this.attributes = attributes;
3032
this.events = events;
33+
this.dataVersion = Optional.empty();
3134
}
3235

3336
public Map<Long, AttributeState> getAttributeStates() {
@@ -38,6 +41,14 @@ public Map<Long, EventState> getEventStates() {
3841
return events;
3942
}
4043

44+
public void setDataVersion(int version) {
45+
dataVersion = Optional.of(version);
46+
}
47+
48+
public Optional<Integer> getDataVersion() {
49+
return dataVersion;
50+
}
51+
4152
/**
4253
* Convenience utility for getting an {@code AttributeState}.
4354
*

src/controller/java/src/chip/devicecontroller/model/NodeState.java

+10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public Map<Integer, EndpointState> getEndpointStates() {
3333
return endpoints;
3434
}
3535

36+
// Called from native code only, which ignores access modifiers.
37+
private void setDataVersion(int endpointId, long clusterId, int dataVersion) {
38+
EndpointState endpointState = getEndpointState(endpointId);
39+
ClusterState clusterState = endpointState.getClusterState(clusterId);
40+
41+
if (clusterState != null) {
42+
clusterState.setDataVersion(dataVersion);
43+
}
44+
}
45+
3646
// Called from native code only, which ignores access modifiers.
3747
private void addAttribute(
3848
int endpointId, long clusterId, long attributeId, AttributeState attributeStateToAdd) {

0 commit comments

Comments
 (0)