From 20a36a4e169ef715612199830393dd8fc5905a73 Mon Sep 17 00:00:00 2001 From: Kartik Anand Date: Wed, 23 Nov 2022 20:11:40 +0530 Subject: [PATCH] Revise discovery message callback --- .../samsung/android/aittnative/JniInterface.java | 18 +++++-- android/aitt-native/src/main/jni/aitt_jni.cc | 57 +++++++++++++++++++--- android/aitt-native/src/main/jni/aitt_jni.h | 7 ++- 3 files changed, 69 insertions(+), 13 deletions(-) diff --git a/android/aitt-native/src/main/java/com/samsung/android/aittnative/JniInterface.java b/android/aitt-native/src/main/java/com/samsung/android/aittnative/JniInterface.java index b6ef57f..ca4dade 100644 --- a/android/aitt-native/src/main/java/com/samsung/android/aittnative/JniInterface.java +++ b/android/aitt-native/src/main/java/com/samsung/android/aittnative/JniInterface.java @@ -18,6 +18,10 @@ package com.samsung.android.aittnative; import android.util.Log; import android.util.Pair; +import com.google.flatbuffers.FlexBuffers; +import com.google.flatbuffers.FlexBuffersBuilder; + +import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -176,7 +180,7 @@ public class JniInterface { * @param discoveryMessage ByteArray containing discovery information */ public void updateDiscoveryMessage(String topic, byte[] discoveryMessage) { - //ToDO: Finalize discovery message format + updateDiscoveryMessageJNI(instance, topic, discoveryMessage, discoveryMessage.length); } /** @@ -210,8 +214,13 @@ public class JniInterface { } } - private void discoveryMessageCallback(String status, byte[] payload) { - //ToDo: Finalize discovery message format. The topic should include topic information to find the right callback. + void discoveryMessageCallback(String topic, String status, byte[] message) { + synchronized (this) { + Pair pair = discoveryCallbacks.get(topic); + if (pair != null) { + pair.second.onDiscoveryMessageReceived(status, message); + } + } } /** @@ -260,6 +269,9 @@ public class JniInterface { /* Native API for removing discovery callback */ private native void removeDiscoveryCallbackJNI(long instance, int cbHandle); + /* Native API for updating discovery message */ + private native void updateDiscoveryMessageJNI(long instance, final String topic, final byte[] data, long datalen); + /* Native API for publishing to a topic */ private native void publishJNI(long instance, final String topic, final byte[] data, long datalen, int protocol, int qos, boolean retain); diff --git a/android/aitt-native/src/main/jni/aitt_jni.cc b/android/aitt-native/src/main/jni/aitt_jni.cc index f5cdea4..f0665af 100644 --- a/android/aitt-native/src/main/jni/aitt_jni.cc +++ b/android/aitt-native/src/main/jni/aitt_jni.cc @@ -378,14 +378,14 @@ jint AittNativeInterface::SetDiscoveryCallback(JNIEnv *env, jobject jni_interfac } int cb = instance->discovery->AddDiscoveryCB(_topic, - std::bind(&AittNativeInterface::DiscoveryMessageCallback, instance, std::placeholders::_1, - std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); + std::bind(&AittNativeInterface::DiscoveryMessageCallback, instance, _topic, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); return (jint) cb; } /** - * JNI API to set the discovery callback with aitt C++ + * JNI API to remove the discovery callback with aitt C++ * @param env JNI interface pointer * @param jni_interface_object JNI interface object * @param handle AittNativeInterface object @@ -402,8 +402,41 @@ void AittNativeInterface::RemoveDiscoveryCallback(JNIEnv *env, jobject jni_inter instance->discovery->RemoveDiscoveryCB(cbHandle); } -void AittNativeInterface::DiscoveryMessageCallback(const std::string &clientId, const std::string &status, - const void *msg, const int szmsg) +/** + * JNI API to update discovery message + * @param env JNI interface pointer + * @param jni_interface_object JNI interface object + * @param handle AittNativeInterface object + * @param topic String for which discovery message to be updated + * @param data ByteArray containing discovery message + * @param data_len int length of discovery message + */ +void AittNativeInterface::UpdateDiscoveryMessage(JNIEnv *env, jobject jni_interface_object, + jlong handle, jstring topic, jbyteArray data, jlong data_len) +{ + if (!CheckParams(env, jni_interface_object)) { + return; + } + + auto *instance = reinterpret_cast(handle); + std::string _topic = GetStringUTF(env, topic); + if (_topic.empty()) { + return; + } + + int num_bytes = (int)data_len; + const char *cdata = (char *)env->GetByteArrayElements(data, nullptr); + if (cdata == nullptr) { + JNI_LOG(ANDROID_LOG_ERROR, TAG, "Failed to get byte array elements"); + return; + } + const void *_data = reinterpret_cast(cdata); + + instance->discovery->UpdateDiscoveryMsg(_topic, _data, num_bytes); +} + +void AittNativeInterface::DiscoveryMessageCallback(const std::string &topic, const std::string &clientId, + const std::string &status, const void *msg, const int szmsg) { JNIEnv *env; int JNIStatus = cbContext.jvm->GetEnv(reinterpret_cast(&env), JNI_VERSION_1_6); @@ -413,6 +446,13 @@ void AittNativeInterface::DiscoveryMessageCallback(const std::string &clientId, } if (env != nullptr) { + jstring _topic = env->NewStringUTF(topic.c_str()); + if (env->ExceptionCheck() == true) { + JNI_LOG(ANDROID_LOG_ERROR, TAG, "Failed to create new UTF string"); + cbContext.jvm->DetachCurrentThread(); + return; + } + jstring _status = env->NewStringUTF(status.c_str()); if (env->ExceptionCheck() == true) { JNI_LOG(ANDROID_LOG_ERROR, TAG, "Failed to create new UTF string"); @@ -429,7 +469,7 @@ void AittNativeInterface::DiscoveryMessageCallback(const std::string &clientId, return; } - env->CallVoidMethod(cbObject, cbContext.discoveryCallbackMethodID, _status, array); + env->CallVoidMethod(cbObject, cbContext.discoveryCallbackMethodID, _topic, _status, array); if (env->ExceptionCheck() == true) { JNI_LOG(ANDROID_LOG_ERROR, TAG, "Failed to call void method"); cbContext.jvm->DetachCurrentThread(); @@ -492,7 +532,7 @@ jlong AittNativeInterface::Init(JNIEnv *env, jobject jni_interface_object, jstri cbContext.connectionCallbackMethodID = env->GetMethodID(callbackClass, "connectionStatusCallback", "(I)V"); cbContext.discoveryCallbackMethodID = - env->GetMethodID(callbackClass, "discoveryMessageCallback", "(Ljava/lang/String;[B)V"); + env->GetMethodID(callbackClass, "discoveryMessageCallback", "(Ljava/lang/String;Ljava/lang/String;[B)V"); env->DeleteLocalRef(callbackClass); } catch (std::exception &e) { JNI_LOG(ANDROID_LOG_ERROR, TAG, e.what()); @@ -529,7 +569,8 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { {"disconnectJNI", "(J)V", reinterpret_cast(AittNativeInterface::Disconnect)}, {"setConnectionCallbackJNI", "(J)V", reinterpret_cast(AittNativeInterface::SetConnectionCallback)}, {"setDiscoveryCallbackJNI", "(JLjava/lang/String;)I", reinterpret_cast(AittNativeInterface::SetDiscoveryCallback)}, - {"removeDiscoveryCallbackJNI", "(JI)V", reinterpret_cast(AittNativeInterface::RemoveDiscoveryCallback)}}; + {"removeDiscoveryCallbackJNI", "(JI)V", reinterpret_cast(AittNativeInterface::RemoveDiscoveryCallback)}, + {"updateDiscoveryMessageJNI", "(JLjava/lang/String;[BJ)V", reinterpret_cast(AittNativeInterface::UpdateDiscoveryMessage)}}; if (env->RegisterNatives(klass, aitt_jni_methods, sizeof(aitt_jni_methods) / sizeof(aitt_jni_methods[0]))) { env->DeleteLocalRef(klass); diff --git a/android/aitt-native/src/main/jni/aitt_jni.h b/android/aitt-native/src/main/jni/aitt_jni.h index 312a267..2ec80e8 100644 --- a/android/aitt-native/src/main/jni/aitt_jni.h +++ b/android/aitt-native/src/main/jni/aitt_jni.h @@ -42,8 +42,8 @@ private: virtual ~AittNativeInterface(void); - void DiscoveryMessageCallback(const std::string &clientId, const std::string &status, - const void *msg, const int szmsg); + void DiscoveryMessageCallback(const std::string &topic, const std::string &clientId, + const std::string &status, const void *msg, const int szmsg); static std::string GetStringUTF(JNIEnv *env, jstring str); @@ -76,6 +76,9 @@ public: static void RemoveDiscoveryCallback(JNIEnv *env, jobject jniInterfaceObject, jlong handle, jint cbHandle); + static void UpdateDiscoveryMessage(JNIEnv *env, jobject jniInterfaceObject, jlong handle, + jstring topic, jbyteArray data, jlong datalen); + private: AITT aitt; AittDiscovery *discovery; -- 2.7.4