Wrong arguments for JNI call

Two bugs are addressed here:
1. The original code passed 3 arguments to a method expecting two.
2. The UUID Java class expects the UUID bytes to be encoded as big-
   endian into the longs.

Test: Manual verification
Change-Id: Id597f31749ed8ff5be4b910c044f17df1be4da78
This commit is contained in:
Ytai Ben-Tsvi
2022-01-26 17:21:20 -08:00
parent 2fccc3a6bf
commit b7a10b7c78

View File

@@ -133,6 +133,18 @@ nativeClassInit (JNIEnv *_env, jclass _this)
MakeGlobalRefOrDie(_env, _env->CallObjectMethod(empty.get(), stringOffsets.intern));
}
uint64_t htonll(uint64_t ll) {
constexpr uint32_t kBytesToTest = 0x12345678;
constexpr uint8_t kFirstByte = (const uint8_t &)kBytesToTest;
constexpr bool kIsLittleEndian = kFirstByte == 0x78;
if constexpr (kIsLittleEndian) {
return static_cast<uint64_t>(htonl(ll & 0xffffffff)) << 32 | htonl(ll >> 32);
} else {
return ll;
}
}
static jstring getJavaInternedString(JNIEnv *env, const String8 &string) {
if (string == "") {
return gStringOffsets.emptyString;
@@ -193,7 +205,8 @@ translateNativeSensorToJavaSensor(JNIEnv *env, jobject sensor, const Sensor& nat
int32_t id = nativeSensor.getId();
env->CallVoidMethod(sensor, sensorOffsets.setId, id);
Sensor::uuid_t uuid = nativeSensor.getUuid();
env->CallVoidMethod(sensor, sensorOffsets.setUuid, id, uuid.i64[0], uuid.i64[1]);
env->CallVoidMethod(sensor, sensorOffsets.setUuid, htonll(uuid.i64[0]),
htonll(uuid.i64[1]));
}
return sensor;
}