Revise discovery message callback
authorKartik Anand <kartik.anand@samsung.com>
Wed, 23 Nov 2022 14:41:40 +0000 (20:11 +0530)
committerKartik Anand <kartik.anand@samsung.com>
Mon, 12 Dec 2022 09:23:52 +0000 (18:23 +0900)
android/aitt-native/src/main/java/com/samsung/android/aittnative/JniInterface.java
android/aitt-native/src/main/jni/aitt_jni.cc
android/aitt-native/src/main/jni/aitt_jni.h

index b6ef57f393b68715223e86fdd2d05fac63c324ec..ca4dade98adafcf497f5453706e480397d2626a7 100644 (file)
@@ -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<Integer, JniDiscoveryCallback> 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);
 
index f5cdea497d2e7e52251d498a4381a505b84cdf6f..f0665af9cf7a1ad65aff51660e580b290d7e1f75 100644 (file)
@@ -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<AittNativeInterface *>(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<const void *>(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<void **>(&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<void *>(AittNativeInterface::Disconnect)},
             {"setConnectionCallbackJNI",    "(J)V",                                     reinterpret_cast<void *>(AittNativeInterface::SetConnectionCallback)},
             {"setDiscoveryCallbackJNI",     "(JLjava/lang/String;)I",                   reinterpret_cast<void *>(AittNativeInterface::SetDiscoveryCallback)},
-            {"removeDiscoveryCallbackJNI",  "(JI)V",                                    reinterpret_cast<void *>(AittNativeInterface::RemoveDiscoveryCallback)}};
+            {"removeDiscoveryCallbackJNI",  "(JI)V",                                    reinterpret_cast<void *>(AittNativeInterface::RemoveDiscoveryCallback)},
+            {"updateDiscoveryMessageJNI",   "(JLjava/lang/String;[BJ)V",                reinterpret_cast<void *>(AittNativeInterface::UpdateDiscoveryMessage)}};
     if (env->RegisterNatives(klass, aitt_jni_methods,
                              sizeof(aitt_jni_methods) / sizeof(aitt_jni_methods[0]))) {
         env->DeleteLocalRef(klass);
index 312a2670d021ff62d6de9bee3998608efe61082b..2ec80e8521bfc7725e2de88ca5b59e0501956a11 100644 (file)
@@ -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;