Skip to content

Commit

Permalink
[Android, Java] Fix JNI Type casting issue (#28464)
Browse files Browse the repository at this point in the history
* Fix Jni Typecasting issue

* Restyled by clang-format

* Update ZAP version

* Zap generated

---------

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Aug 24, 2023
1 parent fc8e76d commit 2064050
Show file tree
Hide file tree
Showing 7 changed files with 8,723 additions and 5,912 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ CHIP_ERROR convertTargetEndpointInfoToJContentApp(TargetEndpointInfo * targetEnd
chip::JniReferences::GetInstance().CreateArrayList(jClustersArrayList);
for (size_t i = 0; i < kMaxNumberOfClustersPerEndpoint && clusters[i] != chip::kInvalidClusterId; i++)
{
jobject jCluster = nullptr;
chip::JniReferences::GetInstance().CreateBoxedObject<uint32_t>("java/lang/Integer", "(I)V", clusters[i], jCluster);
jobject jCluster = nullptr;
jint jniclusterId = static_cast<jint>(clusters[i]);
chip::JniReferences::GetInstance().CreateBoxedObject<jint>("java/lang/Integer", "(I)V", jniclusterId, jCluster);
chip::JniReferences::GetInstance().AddToList(jClustersArrayList, jCluster);
}
}
Expand Down
48 changes: 27 additions & 21 deletions src/controller/java/AndroidDeviceControllerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,9 @@ void AndroidDeviceControllerWrapper::OnScanNetworksSuccess(
jobject NetworkingStatus;
std::string NetworkingStatusClassName = "java/lang/Integer";
std::string NetworkingStatusCtorSignature = "(I)V";
chip::JniReferences::GetInstance().CreateBoxedObject<uint8_t>(
NetworkingStatusClassName.c_str(), NetworkingStatusCtorSignature.c_str(),
static_cast<uint8_t>(dataResponse.networkingStatus), NetworkingStatus);
jint jniNetworkingStatus = static_cast<jint>(dataResponse.networkingStatus);
chip::JniReferences::GetInstance().CreateBoxedObject<jint>(
NetworkingStatusClassName.c_str(), NetworkingStatusCtorSignature.c_str(), jniNetworkingStatus, NetworkingStatus);
jobject DebugText;
if (!dataResponse.debugText.HasValue())
{
Expand Down Expand Up @@ -630,9 +630,10 @@ void AndroidDeviceControllerWrapper::OnScanNetworksSuccess(
jobject newElement_security;
std::string newElement_securityClassName = "java/lang/Integer";
std::string newElement_securityCtorSignature = "(I)V";
chip::JniReferences::GetInstance().CreateBoxedObject<uint8_t>(newElement_securityClassName.c_str(),
newElement_securityCtorSignature.c_str(),
entry.security.Raw(), newElement_security);
jint jniNewElementSecurity = static_cast<jint>(entry.security.Raw());
chip::JniReferences::GetInstance().CreateBoxedObject<jint>(newElement_securityClassName.c_str(),
newElement_securityCtorSignature.c_str(),
jniNewElementSecurity, newElement_security);
jobject newElement_ssid;
jbyteArray newElement_ssidByteArray = env->NewByteArray(static_cast<jsize>(entry.ssid.size()));
env->SetByteArrayRegion(newElement_ssidByteArray, 0, static_cast<jsize>(entry.ssid.size()),
Expand All @@ -644,13 +645,15 @@ void AndroidDeviceControllerWrapper::OnScanNetworksSuccess(
reinterpret_cast<const jbyte *>(entry.bssid.data()));
newElement_bssid = newElement_bssidByteArray;
jobject newElement_channel;
chip::JniReferences::GetInstance().CreateBoxedObject<uint16_t>("java/lang/Integer", "(I)V", entry.channel,
newElement_channel);
jint jniChannel = static_cast<jint>(entry.channel);
chip::JniReferences::GetInstance().CreateBoxedObject<jint>("java/lang/Integer", "(I)V", jniChannel, newElement_channel);
jobject newElement_wiFiBand;
chip::JniReferences::GetInstance().CreateBoxedObject<uint8_t>(
"java/lang/Integer", "(I)V", static_cast<uint8_t>(entry.wiFiBand), newElement_wiFiBand);
jint jniWiFiBand = static_cast<jint>(entry.wiFiBand);
chip::JniReferences::GetInstance().CreateBoxedObject<jint>("java/lang/Integer", "(I)V", jniWiFiBand,
newElement_wiFiBand);
jobject newElement_rssi;
chip::JniReferences::GetInstance().CreateBoxedObject<int8_t>("java/lang/Integer", "(I)V", entry.rssi, newElement_rssi);
jint jniRssi = static_cast<jint>(entry.rssi);
chip::JniReferences::GetInstance().CreateBoxedObject<jint>("java/lang/Integer", "(I)V", jniRssi, newElement_rssi);

jclass wiFiInterfaceScanResultStructClass;
err = chip::JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/WiFiScanResult",
Expand Down Expand Up @@ -691,28 +694,31 @@ void AndroidDeviceControllerWrapper::OnScanNetworksSuccess(
{
auto & entry = iter_ThreadScanResultsInsideOptional.GetValue();
jobject newElement_panId;
chip::JniReferences::GetInstance().CreateBoxedObject<uint16_t>("java/lang/Integer", "(I)V", entry.panId,
newElement_panId);
jint jniPanId = static_cast<jint>(entry.panId);
chip::JniReferences::GetInstance().CreateBoxedObject<jint>("java/lang/Integer", "(I)V", jniPanId, newElement_panId);
jobject newElement_extendedPanId;
chip::JniReferences::GetInstance().CreateBoxedObject<uint64_t>("java/lang/Long", "(J)V", entry.extendedPanId,
newElement_extendedPanId);
jlong jniExtendedPanId = static_cast<jlong>(entry.extendedPanId);
chip::JniReferences::GetInstance().CreateBoxedObject<jlong>("java/lang/Long", "(J)V", jniExtendedPanId,
newElement_extendedPanId);
jobject newElement_networkName;
newElement_networkName = env->NewStringUTF(std::string(entry.networkName.data(), entry.networkName.size()).c_str());
jobject newElement_channel;
chip::JniReferences::GetInstance().CreateBoxedObject<uint16_t>("java/lang/Integer", "(I)V", entry.channel,
newElement_channel);
jint jniChannel = static_cast<jint>(entry.channel);
chip::JniReferences::GetInstance().CreateBoxedObject<jint>("java/lang/Integer", "(I)V", jniChannel, newElement_channel);
jobject newElement_version;
chip::JniReferences::GetInstance().CreateBoxedObject<uint8_t>("java/lang/Integer", "(I)V", entry.version,
newElement_version);
jint jniVersion = static_cast<jint>(entry.version);
chip::JniReferences::GetInstance().CreateBoxedObject<jint>("java/lang/Integer", "(I)V", jniVersion, newElement_version);
jobject newElement_extendedAddress;
jbyteArray newElement_extendedAddressByteArray = env->NewByteArray(static_cast<jsize>(entry.extendedAddress.size()));
env->SetByteArrayRegion(newElement_extendedAddressByteArray, 0, static_cast<jsize>(entry.extendedAddress.size()),
reinterpret_cast<const jbyte *>(entry.extendedAddress.data()));
newElement_extendedAddress = newElement_extendedAddressByteArray;
jobject newElement_rssi;
chip::JniReferences::GetInstance().CreateBoxedObject<int8_t>("java/lang/Integer", "(I)V", entry.rssi, newElement_rssi);
jint jniRssi = static_cast<jint>(entry.rssi);
chip::JniReferences::GetInstance().CreateBoxedObject<jint>("java/lang/Integer", "(I)V", jniRssi, newElement_rssi);
jobject newElement_lqi;
chip::JniReferences::GetInstance().CreateBoxedObject<uint8_t>("java/lang/Integer", "(I)V", entry.lqi, newElement_lqi);
jint jniLqi = static_cast<jint>(entry.lqi);
chip::JniReferences::GetInstance().CreateBoxedObject<jint>("java/lang/Integer", "(I)V", jniLqi, newElement_lqi);

jclass threadInterfaceScanResultStructClass;
err = chip::JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/ThreadScanResult",
Expand Down
9 changes: 6 additions & 3 deletions src/controller/java/templates/partials/decode_value.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ if ({{source}}.IsNull()) {
{{#if_is_strongly_typed_chip_enum type}}
std::string {{target}}ClassName = "{{asJniClassName type null (asUpperCamelCase cluster)}}";
std::string {{target}}CtorSignature = "({{asJniSignature type null (asUpperCamelCase cluster) false}})V";
chip::JniReferences::GetInstance().CreateBoxedObject<{{asUnderlyingBasicType type}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), static_cast<{{asUnderlyingBasicType type}}>({{source}}), {{target}});
{{asJniBasicType type false}} jni{{target}} = static_cast<{{asJniBasicType type false}}>({{source}});
chip::JniReferences::GetInstance().CreateBoxedObject<{{asJniBasicType type false}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), jni{{target}}, {{target}});
{{else}}
{{#if (isOctetString type)}}
jbyteArray {{target}}ByteArray = env->NewByteArray(static_cast<jsize>({{source}}.size()));
Expand All @@ -65,9 +66,11 @@ if ({{source}}.IsNull()) {
std::string {{target}}ClassName = "{{asJniClassName type null (asUpperCamelCase cluster)}}";
std::string {{target}}CtorSignature = "({{asJniSignature type null (asUpperCamelCase cluster) false}})V";
{{#if_is_strongly_typed_bitmap type}}
chip::JniReferences::GetInstance().CreateBoxedObject<{{asUnderlyingBasicType type}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), {{source}}.Raw(), {{target}});
{{asJniBasicType type false}} jni{{target}} = static_cast<{{asJniBasicType type false}}>({{source}}.Raw());
chip::JniReferences::GetInstance().CreateBoxedObject<{{asJniBasicType type false}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), jni{{target}}, {{target}});
{{else}}
chip::JniReferences::GetInstance().CreateBoxedObject<{{asUnderlyingBasicType type}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), {{source}}, {{target}});
{{asJniBasicType type false}} jni{{target}} = static_cast<{{asJniBasicType type false}}>({{source}});
chip::JniReferences::GetInstance().CreateBoxedObject<{{asJniBasicType type false}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), jni{{target}}, {{target}});
{{/if_is_strongly_typed_bitmap}}
{{/if}}
{{/if_is_strongly_typed_chip_enum}}
Expand Down
Loading

0 comments on commit 2064050

Please sign in to comment.