Skip to content

Commit

Permalink
Fix JNI type casts in matter jni layer
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google committed May 13, 2023
1 parent 42f38de commit 9c44788
Show file tree
Hide file tree
Showing 19 changed files with 51 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ class WildcardFragment : Fragment() {

private suspend fun readCurrentFabricIndex() : UInt {
val context = requireContext()
val endpointId = 0L
val endpointId = 0
val clusterId = 62L // OperationalCredentials
val attributeId = 5L // CurrentFabricIndex
val deviceId = addressUpdateFragment.deviceId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ContentAppEndpointManagerImpl(Context context) {
this.context = context;
}

public String sendCommand(int endpointId, int clusterId, int commandId, String commandPayload) {
public String sendCommand(int endpointId, long clusterId, long commandId, String commandPayload) {
Log.d(TAG, "Received a command for endpointId " + endpointId + ". Message " + commandPayload);

ContentApp discoveredApp =
Expand Down Expand Up @@ -54,7 +54,7 @@ public String sendCommand(int endpointId, int clusterId, int commandId, String c
return "Success";
}

public String readAttribute(int endpointId, int clusterId, int attributeId) {
public String readAttribute(int endpointId, long clusterId, long attributeId) {
Log.d(
TAG,
"Received a attribute read request for endpointId "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ std::string ContentAppAttributeDelegate::Read(const chip::app::ConcreteReadAttri

jstring resp =
(jstring) env->CallObjectMethod(mContentAppEndpointManager, mReadAttributeMethod, static_cast<jint>(aPath.mEndpointId),
static_cast<jint>(aPath.mClusterId), static_cast<jint>(aPath.mAttributeId));
static_cast<jlong>(aPath.mClusterId), static_cast<jlong>(aPath.mAttributeId));
if (env->ExceptionCheck())
{
ChipLogError(Zcl, "Java exception in ContentAppAttributeDelegate::Read");
Expand Down
2 changes: 1 addition & 1 deletion examples/tv-app/android/java/ContentAppAttributeDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ContentAppAttributeDelegate
VerifyOrReturn(ContentAppEndpointManagerClass != nullptr,
ChipLogError(Zcl, "Failed to get ContentAppEndpointManager Java class"));

mReadAttributeMethod = env->GetMethodID(ContentAppEndpointManagerClass, "readAttribute", "(III)Ljava/lang/String;");
mReadAttributeMethod = env->GetMethodID(ContentAppEndpointManagerClass, "readAttribute", "(IJJ)Ljava/lang/String;");
if (mReadAttributeMethod == nullptr)
{
ChipLogError(Zcl, "Failed to access ContentAppEndpointManager 'readAttribute' method");
Expand Down
4 changes: 2 additions & 2 deletions examples/tv-app/android/java/ContentAppCommandDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void ContentAppCommandDelegate::InvokeCommand(CommandHandlerInterface::HandlerCo

jstring resp = (jstring) env->CallObjectMethod(
mContentAppEndpointManager, mSendCommandMethod, static_cast<jint>(handlerContext.mRequestPath.mEndpointId),
static_cast<jint>(handlerContext.mRequestPath.mClusterId), static_cast<jint>(handlerContext.mRequestPath.mCommandId),
static_cast<jlong>(handlerContext.mRequestPath.mClusterId), static_cast<jlong>(handlerContext.mRequestPath.mCommandId),
jsonString.jniValue());
if (env->ExceptionCheck())
{
Expand Down Expand Up @@ -108,7 +108,7 @@ Status ContentAppCommandDelegate::InvokeCommand(EndpointId epId, ClusterId clust

jstring resp =
(jstring) env->CallObjectMethod(mContentAppEndpointManager, mSendCommandMethod, static_cast<jint>(epId),
static_cast<jint>(clusterId), static_cast<jint>(commandId), jsonString.jniValue());
static_cast<jlong>(clusterId), static_cast<jlong>(commandId), jsonString.jniValue());
if (env->ExceptionCheck())
{
ChipLogError(Zcl, "Java exception in ContentAppCommandDelegate::sendCommand");
Expand Down
2 changes: 1 addition & 1 deletion examples/tv-app/android/java/ContentAppCommandDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class ContentAppCommandDelegate : public CommandHandlerInterface
ChipLogError(Zcl, "Failed to get ContentAppEndpointManager Java class"));

mSendCommandMethod =
env->GetMethodID(ContentAppEndpointManagerClass, "sendCommand", "(IIILjava/lang/String;)Ljava/lang/String;");
env->GetMethodID(ContentAppEndpointManagerClass, "sendCommand", "(IJJLjava/lang/String;)Ljava/lang/String;");
if (mSendCommandMethod == nullptr)
{
ChipLogError(Zcl, "Failed to access ContentAppEndpointManager 'sendCommand' method");
Expand Down
4 changes: 2 additions & 2 deletions examples/tv-app/android/java/TVApp-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void TvAppJNI::InitializeWithObjects(jobject app)
jclass managerClass = env->GetObjectClass(mTvAppObject);
VerifyOrReturn(managerClass != nullptr, ChipLogError(Zcl, "Failed to get TvAppJNI Java class"));

mPostClusterInitMethod = env->GetMethodID(managerClass, "postClusterInit", "(II)V");
mPostClusterInitMethod = env->GetMethodID(managerClass, "postClusterInit", "(JI)V");
if (mPostClusterInitMethod == nullptr)
{
ChipLogError(Zcl, "Failed to access ChannelManager 'postClusterInit' method");
Expand All @@ -79,7 +79,7 @@ void TvAppJNI::PostClusterInit(int clusterId, int endpoint)
VerifyOrReturn(mTvAppObject != nullptr, ChipLogError(Zcl, "TvAppJNI::mTvAppObject null"));
VerifyOrReturn(mPostClusterInitMethod != nullptr, ChipLogError(Zcl, "TvAppJNI::mPostClusterInitMethod null"));

env->CallVoidMethod(mTvAppObject, mPostClusterInitMethod, static_cast<jint>(clusterId), static_cast<jint>(endpoint));
env->CallVoidMethod(mTvAppObject, mPostClusterInitMethod, static_cast<jlong>(clusterId), static_cast<jint>(endpoint));
if (env->ExceptionCheck())
{
ChipLogError(Zcl, "Failed to call TvAppJNI 'postClusterInit' method");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public TvApp(TvAppCallback callback) {
nativeInit();
}

private void postClusterInit(int clusterId, int endpoint) {
private void postClusterInit(long clusterId, int endpoint) {
Log.d(TAG, "postClusterInit for " + clusterId + " at " + endpoint);
if (mCallback != null) {
mCallback.onClusterInit(this, clusterId, endpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
package com.matter.tv.server.tvapp;

public interface TvAppCallback {
void onClusterInit(TvApp app, int clusterId, int endpoint);
void onClusterInit(TvApp app, long clusterId, int endpoint);
}
45 changes: 25 additions & 20 deletions src/controller/java/AndroidCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,10 @@ void ReportCallback::UpdateClusterDataVersion()

// SetDataVersion to NodeState
jmethodID setDataVersionMethod;
CHIP_ERROR err = JniReferences::GetInstance().FindMethod(env, mNodeStateObj, "setDataVersion", "(IJI)V", &setDataVersionMethod);
CHIP_ERROR err = JniReferences::GetInstance().FindMethod(env, mNodeStateObj, "setDataVersion", "(IJJ)V", &setDataVersionMethod);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find setDataVersion method"));
env->CallVoidMethod(mNodeStateObj, setDataVersionMethod, static_cast<jint>(lastConcreteClusterPath.mEndpointId),
static_cast<jlong>(lastConcreteClusterPath.mClusterId), static_cast<jint>(committedDataVersion.Value()));
static_cast<jlong>(lastConcreteClusterPath.mClusterId), static_cast<jlong>(committedDataVersion.Value()));
VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe());
}

Expand Down Expand Up @@ -428,11 +428,11 @@ CHIP_ERROR CreateChipAttributePath(const app::ConcreteDataAttributePath & aPath,
JniClass attributePathJniCls(attributePathCls);

jmethodID attributePathCtor =
env->GetStaticMethodID(attributePathCls, "newInstance", "(JJJ)Lchip/devicecontroller/model/ChipAttributePath;");
env->GetStaticMethodID(attributePathCls, "newInstance", "(IJJ)Lchip/devicecontroller/model/ChipAttributePath;");
VerifyOrReturnError(attributePathCtor != nullptr, CHIP_JNI_ERROR_METHOD_NOT_FOUND);

outObj =
env->CallStaticObjectMethod(attributePathCls, attributePathCtor, aPath.mEndpointId, aPath.mClusterId, aPath.mAttributeId);
outObj = env->CallStaticObjectMethod(attributePathCls, attributePathCtor, static_cast<jint>(aPath.mEndpointId),
static_cast<jlong>(aPath.mClusterId), static_cast<jlong>(aPath.mAttributeId));
VerifyOrReturnError(outObj != nullptr, CHIP_JNI_ERROR_NULL_OBJECT);

return err;
Expand All @@ -449,7 +449,7 @@ CHIP_ERROR InvokeCallback::CreateInvokeElement(const app::ConcreteCommandPath &
JniClass invokeElementJniCls(invokeElementCls);

jmethodID invokeElementCtor = env->GetStaticMethodID(invokeElementCls, "newInstance",
"(JJJ[BLjava/lang/String;)Lchip/devicecontroller/model/InvokeElement;");
"(IJJ[BLjava/lang/String;)Lchip/devicecontroller/model/InvokeElement;");
VerifyOrReturnError(!env->ExceptionCheck(), CHIP_JNI_ERROR_EXCEPTION_THROWN);
VerifyOrReturnError(invokeElementCtor != nullptr, CHIP_JNI_ERROR_METHOD_NOT_FOUND);

Expand Down Expand Up @@ -480,13 +480,15 @@ CHIP_ERROR InvokeCallback::CreateInvokeElement(const app::ConcreteCommandPath &
ReturnErrorOnFailure(err);

UtfString jsonString(env, JsonToString(json).c_str());
outObj = env->CallStaticObjectMethod(invokeElementCls, invokeElementCtor, aPath.mEndpointId, aPath.mClusterId,
aPath.mCommandId, jniByteArray.jniValue(), jsonString.jniValue());
outObj = env->CallStaticObjectMethod(invokeElementCls, invokeElementCtor, static_cast<jint>(aPath.mEndpointId),
static_cast<jlong>(aPath.mClusterId), static_cast<jlong>(aPath.mCommandId),
jniByteArray.jniValue(), jsonString.jniValue());
}
else
{
outObj = env->CallStaticObjectMethod(invokeElementCls, invokeElementCtor, aPath.mEndpointId, aPath.mClusterId,
aPath.mCommandId, nullptr, nullptr);
outObj = env->CallStaticObjectMethod(invokeElementCls, invokeElementCtor, static_cast<jint>(aPath.mEndpointId),
static_cast<jlong>(aPath.mClusterId), static_cast<jlong>(aPath.mCommandId), nullptr,
nullptr);
}
VerifyOrReturnError(outObj != nullptr, CHIP_JNI_ERROR_NULL_OBJECT);

Expand All @@ -504,10 +506,11 @@ CHIP_ERROR ReportCallback::CreateChipEventPath(const app::ConcreteEventPath & aP
JniClass eventPathJniCls(eventPathCls);

jmethodID eventPathCtor =
env->GetStaticMethodID(eventPathCls, "newInstance", "(JJJ)Lchip/devicecontroller/model/ChipEventPath;");
env->GetStaticMethodID(eventPathCls, "newInstance", "(IJJ)Lchip/devicecontroller/model/ChipEventPath;");
VerifyOrReturnError(eventPathCtor != nullptr, CHIP_JNI_ERROR_METHOD_NOT_FOUND);

outObj = env->CallStaticObjectMethod(eventPathCls, eventPathCtor, aPath.mEndpointId, aPath.mClusterId, aPath.mEventId);
outObj = env->CallStaticObjectMethod(eventPathCls, eventPathCtor, static_cast<jint>(aPath.mEndpointId),
static_cast<jlong>(aPath.mClusterId), static_cast<jlong>(aPath.mEventId));
VerifyOrReturnError(outObj != nullptr, CHIP_JNI_ERROR_NULL_OBJECT);

return err;
Expand Down Expand Up @@ -558,8 +561,9 @@ CHIP_ERROR ReportCallback::OnResubscriptionNeeded(app::ReadClient * apReadClient
env, mResubscriptionAttemptCallbackRef, "onResubscriptionAttempt", "(II)V", &onResubscriptionAttemptMethod));

DeviceLayer::StackUnlock unlock;
env->CallVoidMethod(mResubscriptionAttemptCallbackRef, onResubscriptionAttemptMethod, aTerminationCause.AsInteger(),
apReadClient->ComputeTimeTillNextSubscription());
env->CallVoidMethod(mResubscriptionAttemptCallbackRef, onResubscriptionAttemptMethod,
static_cast<jint>(aTerminationCause.AsInteger()),
static_cast<jint>(apReadClient->ComputeTimeTillNextSubscription()));
VerifyOrReturnError(!env->ExceptionCheck(), CHIP_JNI_ERROR_EXCEPTION_THROWN);
return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -763,10 +767,11 @@ CHIP_ERROR ReportEventCallback::CreateChipEventPath(const app::ConcreteEventPath
JniClass eventPathJniCls(eventPathCls);

jmethodID eventPathCtor =
env->GetStaticMethodID(eventPathCls, "newInstance", "(JJJ)Lchip/devicecontroller/model/ChipEventPath;");
env->GetStaticMethodID(eventPathCls, "newInstance", "(IJJ)Lchip/devicecontroller/model/ChipEventPath;");
VerifyOrReturnError(eventPathCtor != nullptr, CHIP_JNI_ERROR_METHOD_NOT_FOUND);

outObj = env->CallStaticObjectMethod(eventPathCls, eventPathCtor, aPath.mEndpointId, aPath.mClusterId, aPath.mEventId);
outObj = env->CallStaticObjectMethod(eventPathCls, eventPathCtor, static_cast<jint>(aPath.mEndpointId),
static_cast<jlong>(aPath.mClusterId), static_cast<jlong>(aPath.mEventId));
VerifyOrReturnError(outObj != nullptr, CHIP_JNI_ERROR_NULL_OBJECT);

return err;
Expand Down Expand Up @@ -811,8 +816,9 @@ CHIP_ERROR ReportEventCallback::OnResubscriptionNeeded(app::ReadClient * apReadC
env, mResubscriptionAttemptCallbackRef, "onResubscriptionAttempt", "(II)V", &onResubscriptionAttemptMethod));

DeviceLayer::StackUnlock unlock;
env->CallVoidMethod(mResubscriptionAttemptCallbackRef, onResubscriptionAttemptMethod, aTerminationCause.AsInteger(),
apReadClient->ComputeTimeTillNextSubscription());
env->CallVoidMethod(mResubscriptionAttemptCallbackRef, onResubscriptionAttemptMethod,
static_cast<jint>(aTerminationCause.AsInteger()),
static_cast<jint>(apReadClient->ComputeTimeTillNextSubscription()));

return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -996,8 +1002,7 @@ void InvokeCallback::OnResponse(app::CommandSender * apCommandSender, const app:
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to find onError method: %s", ErrorStr(err)));

DeviceLayer::StackUnlock unlock;
env->CallVoidMethod(mJavaCallbackRef, onResponseMethod, invokeElementObj,
static_cast<std::underlying_type_t<Protocols::InteractionModel::Status>>(aStatusIB.mStatus));
env->CallVoidMethod(mJavaCallbackRef, onResponseMethod, invokeElementObj, static_cast<jlong>(aStatusIB.mStatus));
VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe());
}

Expand Down
2 changes: 1 addition & 1 deletion src/controller/java/AndroidCurrentFabricRemover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ AndroidCurrentFabricRemover::AndroidCurrentFabricRemover(DeviceController * cont
env->ExceptionClear();
}

mOnErrorMethod = env->GetMethodID(callbackClass, "onError", "(IJ)V");
mOnErrorMethod = env->GetMethodID(callbackClass, "onError", "(JI)V");
if (mOnErrorMethod == nullptr)
{
ChipLogError(Controller, "Failed to access callback 'onError' method");
Expand Down
5 changes: 3 additions & 2 deletions src/controller/java/AndroidDeviceControllerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,8 @@ void AndroidDeviceControllerWrapper::OnCommissioningComplete(NodeId deviceId, CH
CHIP_ERROR err = JniReferences::GetInstance().FindMethod(env, mJavaObjectRef, "onCommissioningComplete", "(JI)V",
&onCommissioningCompleteMethod);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Error finding Java method: %" CHIP_ERROR_FORMAT, err.Format()));
env->CallVoidMethod(mJavaObjectRef, onCommissioningCompleteMethod, static_cast<jlong>(deviceId), error.AsInteger());
env->CallVoidMethod(mJavaObjectRef, onCommissioningCompleteMethod, static_cast<jlong>(deviceId),
static_cast<jint>(error.AsInteger()));

if (ssidStr != nullptr)
{
Expand Down Expand Up @@ -560,7 +561,7 @@ void AndroidDeviceControllerWrapper::OnCommissioningStatusUpdate(PeerId peerId,

UtfString jStageCompleted(env, StageToString(stageCompleted));
env->CallVoidMethod(mJavaObjectRef, onCommissioningStatusUpdateMethod, static_cast<jlong>(peerId.GetNodeId()),
jStageCompleted.jniValue(), error.AsInteger());
jStageCompleted.jniValue(), static_cast<jint>(error.AsInteger()));
}

void AndroidDeviceControllerWrapper::OnReadCommissioningInfo(const chip::Controller::ReadCommissioningInfo & info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static AttributeWriteRequest newInstance(

/** Create a new {@link AttributeWriteRequest} with only concrete ids. */
public static AttributeWriteRequest newInstance(
long endpointId, long clusterId, long attributeId, byte[] tlv) {
int endpointId, long clusterId, long attributeId, byte[] tlv) {
return new AttributeWriteRequest(
ChipPathId.forId(endpointId),
ChipPathId.forId(clusterId),
Expand All @@ -117,11 +117,7 @@ public static AttributeWriteRequest newInstance(
}

public static AttributeWriteRequest newInstance(
long endpointId,
long clusterId,
long attributeId,
byte[] tlv,
Optional<Integer> dataVersion) {
int endpointId, long clusterId, long attributeId, byte[] tlv, Optional<Integer> dataVersion) {
return new AttributeWriteRequest(
ChipPathId.forId(endpointId),
ChipPathId.forId(clusterId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static ChipAttributePath newInstance(
}

/** Create a new {@link ChipAttributePath} with only concrete ids. */
public static ChipAttributePath newInstance(long endpointId, long clusterId, long attributeId) {
public static ChipAttributePath newInstance(int endpointId, long clusterId, long attributeId) {
return new ChipAttributePath(
ChipPathId.forId(endpointId), ChipPathId.forId(clusterId), ChipPathId.forId(attributeId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static ChipEventPath newInstance(

/** Create a new {@link ChipEventPath} with only concrete ids. */
public static ChipEventPath newInstance(
long endpointId, long clusterId, long eventId, boolean isUrgent) {
int endpointId, long clusterId, long eventId, boolean isUrgent) {
return new ChipEventPath(
ChipPathId.forId(endpointId),
ChipPathId.forId(clusterId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public final class ClusterState {
private Map<Long, AttributeState> attributes;
private Map<Long, ArrayList<EventState>> events;
private Optional<Integer> dataVersion;
private Optional<Long> dataVersion;

public ClusterState(
Map<Long, AttributeState> attributes, Map<Long, ArrayList<EventState>> events) {
Expand All @@ -43,11 +43,11 @@ public Map<Long, ArrayList<EventState>> getEventStates() {
return events;
}

public void setDataVersion(int version) {
public void setDataVersion(long version) {
dataVersion = Optional.of(version);
}

public Optional<Integer> getDataVersion() {
public Optional<Long> getDataVersion() {
return dataVersion;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static InvokeElement newInstance(

/** Create a new {@link InvokeElement} with only concrete ids. */
public static InvokeElement newInstance(
long endpointId,
int endpointId,
long clusterId,
long commandId,
@Nullable byte[] tlv,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Map<Integer, EndpointState> getEndpointStates() {
}

// Called from native code only, which ignores access modifiers.
private void setDataVersion(int endpointId, long clusterId, int dataVersion) {
private void setDataVersion(int endpointId, long clusterId, long dataVersion) {
EndpointState endpointState = getEndpointState(endpointId);
ClusterState clusterState = endpointState.getClusterState(clusterId);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/support/JniReferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ CHIP_ERROR JniReferences::CallSubscriptionEstablished(jobject javaCallback, long
&subscriptionEstablishedMethod);
VerifyOrReturnError(err == CHIP_NO_ERROR, CHIP_JNI_ERROR_METHOD_NOT_FOUND);

env->CallVoidMethod(javaCallback, subscriptionEstablishedMethod, subscriptionId);
env->CallVoidMethod(javaCallback, subscriptionEstablishedMethod, static_cast<jlong>(subscriptionId));
VerifyOrReturnError(!env->ExceptionCheck(), CHIP_JNI_ERROR_EXCEPTION_THROWN);

return err;
Expand Down

0 comments on commit 9c44788

Please sign in to comment.