]> sigrok.org Git - libsigrok.git/blobdiff - bindings/java/org/sigrok/core/classes/classes.i
Java: Fill jlongs with 0 when doing SWIG style pointer marshalling
[libsigrok.git] / bindings / java / org / sigrok / core / classes / classes.i
index 5fdbc83e17cb97d590aa2591fb1c7f7ff888adf2..894c72f75b9d501daa582b75e744451588018df8 100644 (file)
@@ -7,17 +7,36 @@
   }
 %}
 
-/* Map Java FileDescriptor objects to int fds */
-%typemap(jni) int fd "jobject"
-%typemap(jtype) int fd "java.io.FileDescriptor"
-%typemap(jstype) int fd "java.io.FileDescriptor"
-%typemap(javain) int fd "$javainput"
-
-%typemap(in) int fd {
-  jclass FileDescriptor = jenv->FindClass("java/io/FileDescriptor");
-  jfieldID fd = jenv->GetFieldID(FileDescriptor, "fd", "I");
-  $1 = jenv->GetIntField($input, fd);
-}
+/* Documentation & importing interfaces. */
+%pragma(java) jniclassimports=%{
+/**
+ * @mainpage API Reference
+ * 
+ * Introduction
+ * ------------
+ * 
+ * The sigrok-java API provides an object-oriented Java interface to the
+ * functionality in libsigrok. It is built on top of the libsigrokcxx C++ API.
+ * 
+ * Getting started
+ * ---------------
+ * 
+ * Usage of the sigrok-java API needs to begin with a call to Context.create().
+ * This will create the global libsigrok context and returns a Context object.
+ * Methods on this object provide access to the hardware drivers, input and
+ * output formats supported by the library, as well as means of creating other
+ * objects such as sessions and triggers.
+ * 
+ * Error handling
+ * --------------
+ * 
+ * When any libsigrok C API call returns an error, an Error exception is raised,
+ * which provides access to the error code and description.
+ */
+
+import org.sigrok.core.interfaces.LogCallback;
+import org.sigrok.core.interfaces.DatafeedCallback;
+%}
 
 /* Map Glib::VariantBase to a Variant class in Java */
 %rename(Variant) VariantBase;
@@ -25,6 +44,8 @@ namespace Glib {
   class VariantBase {};
 }
 
+%include "bindings/swig/templates.i"
+
 /* Map between std::vector and java.util.Vector */
 %define VECTOR(CValue, JValue)
 
@@ -33,20 +54,12 @@ namespace Glib {
 %typemap(jstype) std::vector< CValue > "java.util.Vector<JValue>"
 
 %typemap(javain,
-    pre="  $javaclassname temp$javainput = $javaclassname.convertVector($javainput);",
+    pre="  $javaclassname temp$javainput = new $javaclassname();
+  for (JValue value : $javainput)
+    temp$javainput.add(value);",
     pgcppname="temp$javainput")
   std::vector< CValue > "$javaclassname.getCPtr(temp$javainput)"
 
-%typemap(javacode) std::vector< CValue > %{
-  static $javaclassname convertVector(java.util.Vector<JValue> in)
-  {
-    $javaclassname out = new $javaclassname();
-    for (JValue value : in)
-      out.add(value);
-    return out;
-  }
-%}
-
 %typemap(javaout) std::vector< CValue > {
   return (java.util.Vector<JValue>)$jnicall;
 }
@@ -59,7 +72,7 @@ namespace Glib {
   jclass Value = jenv->FindClass("org/sigrok/core/classes/" #JValue);
   jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
   $result = jenv->NewObject(Vector, Vector_init);
-  jlong value;
+  jlong value = 0;
   for (auto entry : $1)
   {
     *(CValue **) &value = new CValue(entry);
@@ -81,20 +94,12 @@ VECTOR(std::shared_ptr<sigrok::HardwareDevice>, HardwareDevice)
   "java.util.Map<JKey, JValue>"
 
 %typemap(javain,
-    pre="  $javaclassname temp$javainput = $javaclassname.convertMap($javainput);",
+    pre="  $javaclassname temp$javainput = new $javaclassname();
+    for (java.util.Map.Entry<JKey, JValue> entry : $javainput.entrySet())
+      temp$javainput.set(entry.getKey(), entry.getValue());",
     pgcppname="temp$javainput")
   std::map< CKey, CValue > "$javaclassname.getCPtr(temp$javainput)"
 
-%typemap(javacode) std::map< CKey, CValue > %{
-  static $javaclassname convertMap(java.util.Map<JKey,JValue> in)
-  {
-    $javaclassname out = new $javaclassname();
-    for (java.util.Map.Entry<JKey, JValue> entry : in.entrySet())
-      out.set(entry.getKey(), entry.getValue());
-    return out;
-  }
-%}
-
 %typemap(javaout) std::map< CKey, CValue > {
   return (java.util.Map<JKey, JValue>)$jnicall;
 }
@@ -105,6 +110,11 @@ VECTOR(std::shared_ptr<sigrok::HardwareDevice>, HardwareDevice)
 
 MAP_COMMON(std::string, std::string, String, String)
 
+%typemap(jni) std::map<std::string, std::string>
+  "jobject"
+%typemap(jtype) std::map<std::string, std::string>
+  "java.util.Map<String,String>"
+
 %typemap(out) std::map<std::string, std::string> {
   jclass HashMap = jenv->FindClass("java/util/HashMap");
   jmethodID init = jenv->GetMethodID(HashMap, "<init>", "()V");
@@ -136,7 +146,7 @@ MAP_COMMON(std::string, std::shared_ptr<sigrok::ClassName>, String, ClassName)
   jclass Value = jenv->FindClass("org/sigrok/core/classes/" #ClassName);
   jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
   $result = jenv->NewObject(HashMap, HashMap_init);
-  jlong value;
+  jlong value = 0;
   for (auto entry : $1)
   {
     *(std::shared_ptr< sigrok::ClassName > **)&value =
@@ -171,8 +181,8 @@ MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
   jclass Variant = jenv->FindClass("org/sigrok/core/classes/Variant");
   jmethodID Variant_init = jenv->GetMethodID(Variant, "<init>", "(JZ)V");
   $result = jenv->NewObject(HashMap, HashMap_init);
-  jlong key;
-  jlong value;
+  jlong key = 0;
+  jlong value = 0;
   for (auto entry : $1)
   {
     *(const sigrok::ConfigKey **) &key = entry.first;
@@ -183,22 +193,41 @@ MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
   }
 }
 
-/* Support Driver.scan() with no arguments. */
-%extend sigrok::Driver {
-  std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan()
-  {
-    std::map<const sigrok::ConfigKey *, Glib::VariantBase> options;
-    return $self->scan(options);
-  }
-}
+/* Specialisation for ConfigKey->set<Capability> maps */
+
+MAP_COMMON(const sigrok::ConfigKey *, std::set<enum sigrok::Capability>,
+  ConfigKey, java.util.Set<Capability>)
+
+%typemap(jni) std::map<const sigrok::ConfigKey *, std::set<enum sigrok::Capability> > "jobject"
+%typemap(jtype) std::map<const sigrok::ConfigKey *, std::set<enum sigrok::Capability> >
+  "java.util.Map<ConfigKey,java.util.Set<Capability>>"
 
-/* Support OutputFormat.create_output(device) with no options. */
-%extend sigrok::OutputFormat {
-  std::shared_ptr<sigrok::Output> create_output(
-    std::shared_ptr<sigrok::Device> device)
+%typemap(out) std::map<const sigrok::ConfigKey *, std::set<enum sigrok::Capability> > {
+  jclass HashMap = jenv->FindClass("java/util/HashMap");
+  jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
+  jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
+    "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
+  jclass HashSet = jenv->FindClass("java/util/HashSet");
+  jmethodID HashSet_init = jenv->GetMethodID(HashSet, "<init>", "()V");
+  jmethodID HashSet_add = jenv->GetMethodID(HashSet, "add",
+    "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
+  jclass ConfigKey = jenv->FindClass("org/sigrok/core/classes/ConfigKey");
+  jmethodID ConfigKey_init = jenv->GetMethodID(ConfigKey, "<init>", "(JZ)V");
+  jclass Capability = jenv->FindClass("org/sigrok/core/classes/Capability");
+  jmethodID Capability_swigToEnum = jenv->GetStaticMethodID(Capability,
+    "swigToEnum", "(I)Lorg/sigrok/core/classes/Capability;");
+  $result = jenv->NewObject(HashMap, HashMap_init);
+  jlong key = 0;
+  for (auto map_entry : $1)
   {
-    std::map<std::string, std::string> options;
-    return $self->create_output(device, options);
+    *(const sigrok::ConfigKey **) &key = map_entry.first;
+    jobject value = jenv->NewObject(HashSet, HashSet_init);
+    for (auto &set_entry : map_entry.second)
+      jenv->CallObjectMethod(value, HashSet_add,
+        jenv->CallStaticObjectMethod(Capability,
+          Capability_swigToEnum, set_entry));
+    jenv->CallObjectMethod($result, HashMap_put,
+      jenv->NewObject(ConfigKey, ConfigKey_init, key, false), value);
   }
 }
 
@@ -210,6 +239,9 @@ MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
 
 /* Support Java log callbacks. */
 
+%typemap(javaimports) sigrok::Context
+  "import org.sigrok.core.interfaces.LogCallback;"
+
 %inline {
 typedef jobject jlogcallback;
 }
@@ -235,7 +267,7 @@ typedef jobject jlogcallback;
       const sigrok::LogLevel *loglevel,
       std::string message)
     {
-      jlong loglevel_addr;
+      jlong loglevel_addr = 0;
       *(const sigrok::LogLevel **) &loglevel_addr = loglevel;
       jobject loglevel_obj = env->NewObject(
         LogLevel, LogLevel_init, loglevel_addr, false);
@@ -249,6 +281,9 @@ typedef jobject jlogcallback;
 
 /* Support Java datafeed callbacks. */
 
+%typemap(javaimports) sigrok::Session
+  "import org.sigrok.core.interfaces.DatafeedCallback;"
+
 %inline {
 typedef jobject jdatafeedcallback;
 }
@@ -277,8 +312,8 @@ typedef jobject jdatafeedcallback;
       std::shared_ptr<sigrok::Device> device,
       std::shared_ptr<sigrok::Packet> packet)
     {
-      jlong device_addr;
-      jlong packet_addr;
+      jlong device_addr = 0;
+      jlong packet_addr = 0;
       *(std::shared_ptr<sigrok::Device> **) &device_addr =
         new std::shared_ptr<sigrok::Device>(device);
       *(std::shared_ptr<sigrok::Packet> **) &packet_addr =
@@ -294,36 +329,15 @@ typedef jobject jdatafeedcallback;
   }
 }
 
-/* Support Java event source callbacks. */
+%include "doc.i"
 
-%inline {
-typedef jobject jsourcecallback;
-}
-
-%typemap(jni) jsourcecallback "jsourcecallback"
-%typemap(jtype) jsourcecallback "SourceCallback"
-%typemap(jstype) jsourcecallback "SourceCallback"
-%typemap(javain) jsourcecallback "$javainput"
-
-%extend sigrok::EventSource
-{
-  std::shared_ptr<sigrok::EventSource> create(
-    int fd, Glib::IOCondition events, int timeout,
-    JNIEnv *env, jsourcecallback obj)
-  {
-    (void) $self;
-    jclass obj_class = env->GetObjectClass(obj);
-    jmethodID method = env->GetMethodID(obj_class, "run", "(I)V");
-    jobject obj_ref = env->NewGlobalRef(obj);
+%define %enumextras(Class)
+%enddef
 
-    return sigrok::EventSource::create(fd, events, timeout, [=] (int revents)
-    {
-      bool result = env->CallBooleanMethod(obj_ref, method, revents);
-      if (env->ExceptionCheck())
-        throw sigrok::Error(SR_ERR);
-      return result;
-    });
-  }
-}
+/* Ignore these for now, need fixes. */
+%ignore sigrok::Context::create_analog_packet;
+%ignore sigrok::Context::create_meta_packet;
+%ignore sigrok::Meta::config;
 
 %include "bindings/swig/classes.i"
+