3 /* Automatically load JNI library. */
4 %pragma(java) jniclasscode=%{
6 System.loadLibrary("sigrok_java_core_classes");
10 /* Documentation & importing interfaces. */
11 %pragma(java) jniclassimports=%{
13 * @mainpage API Reference
18 * The sigrok-java API provides an object-oriented Java interface to the
19 * functionality in libsigrok. It is built on top of the libsigrokcxx C++ API.
24 * Usage of the sigrok-java API needs to begin with a call to Context.create().
25 * This will create the global libsigrok context and returns a Context object.
26 * Methods on this object provide access to the hardware drivers, input and
27 * output formats supported by the library, as well as means of creating other
28 * objects such as sessions and triggers.
33 * When any libsigrok C API call returns an error, an Error exception is raised,
34 * which provides access to the error code and description.
37 import org.sigrok.core.interfaces.LogCallback;
38 import org.sigrok.core.interfaces.DatafeedCallback;
41 /* Map Glib::VariantBase to a Variant class in Java */
42 %rename(Variant) VariantBase;
47 %include "bindings/swig/templates.i"
49 /* Map between std::vector and java.util.Vector */
50 %define VECTOR(CValue, JValue)
52 %typemap(jni) std::vector< CValue > "jobject"
53 %typemap(jtype) std::vector< CValue > "java.util.Vector<JValue>"
54 %typemap(jstype) std::vector< CValue > "java.util.Vector<JValue>"
57 pre=" $javaclassname temp$javainput = new $javaclassname();
58 for (JValue value : $javainput)
59 temp$javainput.add(value);",
60 pgcppname="temp$javainput")
61 std::vector< CValue > "$javaclassname.getCPtr(temp$javainput)"
63 %typemap(javaout) std::vector< CValue > {
64 return (java.util.Vector<JValue>)$jnicall;
67 %typemap(out) std::vector< CValue > {
68 jclass Vector = jenv->FindClass("java/util/Vector");
69 jmethodID Vector_init = jenv->GetMethodID(Vector, "<init>", "()V");
70 jmethodID Vector_add = jenv->GetMethodID(Vector, "add",
71 "(Ljava/lang/Object;)Z");
72 jclass Value = jenv->FindClass("org/sigrok/core/classes/" #JValue);
73 jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
74 $result = jenv->NewObject(Vector, Vector_init);
78 *(CValue **) &value = new CValue(entry);
79 jenv->CallBooleanMethod($result, Vector_add,
80 jenv->NewObject(Value, Value_init, value, true));
86 VECTOR(std::shared_ptr<sigrok::Channel>, Channel)
87 VECTOR(std::shared_ptr<sigrok::HardwareDevice>, HardwareDevice)
89 /* Common macro for mapping between std::map and java.util.Map */
91 %define MAP_COMMON(CKey, CValue, JKey, JValue)
93 %typemap(jstype) std::map< CKey, CValue >
94 "java.util.Map<JKey, JValue>"
97 /* SWIG 4.0.0 changed the std::map wrappers in an incompatible way. */
98 #if SWIG_VERSION >= 0x040000
99 pre=" $javaclassname temp$javainput = new $javaclassname();
100 for (java.util.Map.Entry<JKey, JValue> entry : $javainput.entrySet())
101 temp$javainput.put(entry.getKey(), entry.getValue());",
102 pgcppname="temp$javainput")
104 pre=" $javaclassname temp$javainput = new $javaclassname();
105 for (java.util.Map.Entry<JKey, JValue> entry : $javainput.entrySet())
106 temp$javainput.set(entry.getKey(), entry.getValue());",
107 pgcppname="temp$javainput")
109 std::map< CKey, CValue > "$javaclassname.getCPtr(temp$javainput)"
111 %typemap(javaout) std::map< CKey, CValue > {
112 return (java.util.Map<JKey, JValue>)$jnicall;
117 /* Specialisation for string->string maps. */
119 MAP_COMMON(std::string, std::string, String, String)
121 %typemap(jni) std::map<std::string, std::string>
123 %typemap(jtype) std::map<std::string, std::string>
124 "java.util.Map<String,String>"
126 %typemap(out) std::map<std::string, std::string> {
127 jclass HashMap = jenv->FindClass("java/util/HashMap");
128 jmethodID init = jenv->GetMethodID(HashMap, "<init>", "()V");
129 jmethodID put = jenv->GetMethodID(HashMap, "put",
130 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
131 $result = jenv->NewObject(HashMap, init);
132 for (auto entry : $1)
133 jenv->CallObjectMethod($result, put,
134 jenv->NewStringUTF(entry.first.c_str()),
135 jenv->NewStringUTF(entry.second.c_str()));
138 /* Specialisation macro for string->shared_ptr maps. */
140 %define STRING_TO_SHARED_PTR_MAP(ClassName)
142 %typemap(jni) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
144 %typemap(jtype) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
145 "java.util.Map<String,ClassName>"
147 MAP_COMMON(std::string, std::shared_ptr<sigrok::ClassName>, String, ClassName)
149 %typemap(out) std::map<std::string, std::shared_ptr<sigrok::ClassName> > {
150 jclass HashMap = jenv->FindClass("java/util/HashMap");
151 jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
152 jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
153 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
154 jclass Value = jenv->FindClass("org/sigrok/core/classes/" #ClassName);
155 jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
156 $result = jenv->NewObject(HashMap, HashMap_init);
158 for (auto entry : $1)
160 *(std::shared_ptr< sigrok::ClassName > **)&value =
161 new std::shared_ptr< sigrok::ClassName>(entry.second);
162 jenv->CallObjectMethod($result, HashMap_put,
163 jenv->NewStringUTF(entry.first.c_str()),
164 jenv->NewObject(Value, Value_init, value, true));
170 STRING_TO_SHARED_PTR_MAP(Driver)
171 STRING_TO_SHARED_PTR_MAP(InputFormat)
172 STRING_TO_SHARED_PTR_MAP(OutputFormat)
174 /* Specialisation for ConfigKey->Variant maps */
176 MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
178 %typemap(jni) std::map<const sigrok::ConfigKey, Glib::VariantBase> "jobject"
179 %typemap(jtype) std::map<const sigrok::ConfigKey, Glib::VariantBase>
180 "java.util.Map<ConfigKey,Variant>"
182 %typemap(out) std::map<const sigrok::ConfigKey *, Glib::VariantBase> {
183 jclass HashMap = jenv->FindClass("java/util/HashMap");
184 jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
185 jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
186 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
187 jclass ConfigKey = jenv->FindClass("org/sigrok/core/classes/ConfigKey");
188 jmethodID ConfigKey_init = jenv->GetMethodID(ConfigKey, "<init>", "(JZ)V");
189 jclass Variant = jenv->FindClass("org/sigrok/core/classes/Variant");
190 jmethodID Variant_init = jenv->GetMethodID(Variant, "<init>", "(JZ)V");
191 $result = jenv->NewObject(HashMap, HashMap_init);
194 for (auto entry : $1)
196 *(const sigrok::ConfigKey **) &key = entry.first;
197 *(Glib::VariantBase **) &value = new Glib::VariantBase(entry.second);
198 jenv->CallObjectMethod($result, HashMap_put,
199 jenv->NewObject(ConfigKey, ConfigKey_init, key, false));
200 jenv->NewObject(Variant, Variant_init, value, true));
204 /* Pass JNIEnv parameter to C++ extension methods requiring it. */
206 %typemap(in, numinputs=0) JNIEnv * %{
210 /* Thread safe JNIEnv handling */
216 ScopedEnv(JavaVM *jvm);
218 JNIEnv* operator-> () { return env; }
219 operator bool () const { return (bool)env; }
225 ScopedEnv::ScopedEnv(JavaVM *jvm) : jvm(jvm), env(NULL) {
226 env_status = jvm->GetEnv((void **)&env, JNI_VERSION_1_2);
227 if (env_status == JNI_EDETACHED) {
228 %#if defined(__ANDROID__)
229 jvm->AttachCurrentThread(&env, NULL);
231 jvm->AttachCurrentThread((void **)&env, NULL);
235 ScopedEnv::~ScopedEnv() {
236 if (env_status == JNI_EDETACHED) {
237 jvm->DetachCurrentThread();
243 /* "Smartpointer" for Java references. */
250 GlobalRefBase (JavaVM *jvm, jobject ref);
255 GlobalRefBase::GlobalRefBase (JavaVM *jvm, jobject ref) : jvm(jvm), jref(0) {
258 jref = env->NewGlobalRef(ref);
260 GlobalRefBase::~GlobalRefBase () {
263 env->DeleteGlobalRef(jref);
265 template <class Jtype>
266 class GlobalRef : private GlobalRefBase
269 GlobalRef (JavaVM *jvm, Jtype ref) : GlobalRefBase(jvm, ref) {}
270 GlobalRef (const GlobalRef &ref) : GlobalRefBase(ref.jvm, ref.jref) {}
271 operator Jtype () const { return static_cast<Jtype>(jref); }
276 /* Support Java log callbacks. */
278 %typemap(javaimports) sigrok::Context
279 "import org.sigrok.core.interfaces.LogCallback;"
282 typedef jobject jlogcallback;
285 %typemap(jni) jlogcallback "jlogcallback"
286 %typemap(jtype) jlogcallback "LogCallback"
287 %typemap(jstype) jlogcallback "LogCallback"
288 %typemap(javain) jlogcallback "$javainput"
290 %extend sigrok::Context
292 void add_log_callback(JNIEnv *env, jlogcallback obj)
295 env->GetJavaVM(&jvm);
296 jclass obj_class = env->GetObjectClass(obj);
297 jmethodID method = env->GetMethodID(obj_class, "run",
298 "(Lorg/sigrok/core/classes/LogLevel;Ljava/lang/String;)V");
299 GlobalRef<jclass> LogLevel(jvm, env->FindClass("org/sigrok/core/classes/LogLevel"));
300 jmethodID LogLevel_init = env->GetMethodID(LogLevel, "<init>", "(JZ)V");
301 GlobalRef<jobject> obj_ref(jvm, obj);
303 $self->set_log_callback([=] (
304 const sigrok::LogLevel *loglevel,
309 throw sigrok::Error(SR_ERR);
310 jlong loglevel_addr = 0;
311 *(const sigrok::LogLevel **) &loglevel_addr = loglevel;
312 jobject loglevel_obj = env->NewObject(
313 LogLevel, LogLevel_init, loglevel_addr, false);
314 jobject message_obj = env->NewStringUTF(message.c_str());
315 env->CallVoidMethod(obj_ref, method, loglevel_obj, message_obj);
316 if (env->ExceptionCheck())
317 throw sigrok::Error(SR_ERR);
322 /* Support Java datafeed callbacks. */
324 %typemap(javaimports) sigrok::Session
325 "import org.sigrok.core.interfaces.DatafeedCallback;"
328 typedef jobject jdatafeedcallback;
331 %typemap(jni) jdatafeedcallback "jdatafeedcallback"
332 %typemap(jtype) jdatafeedcallback "DatafeedCallback"
333 %typemap(jstype) jdatafeedcallback "DatafeedCallback"
334 %typemap(javain) jdatafeedcallback "$javainput"
336 %extend sigrok::Session
338 void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj)
341 env->GetJavaVM(&jvm);
342 jclass obj_class = env->GetObjectClass(obj);
343 jmethodID method = env->GetMethodID(obj_class, "run",
344 "(Lorg/sigrok/core/classes/Device;Lorg/sigrok/core/classes/Packet;)V");
345 GlobalRef<jclass> Device(jvm, env->FindClass("org/sigrok/core/classes/Device"));
346 jmethodID Device_init = env->GetMethodID(Device, "<init>", "(JZ)V");
347 GlobalRef<jclass> Packet(jvm, env->FindClass("org/sigrok/core/classes/Packet"));
348 jmethodID Packet_init = env->GetMethodID(Packet, "<init>", "(JZ)V");
349 GlobalRef<jobject> obj_ref(jvm, obj);
351 $self->add_datafeed_callback([=] (
352 std::shared_ptr<sigrok::Device> device,
353 std::shared_ptr<sigrok::Packet> packet)
357 throw sigrok::Error(SR_ERR);
358 jlong device_addr = 0;
359 jlong packet_addr = 0;
360 *(std::shared_ptr<sigrok::Device> **) &device_addr =
361 new std::shared_ptr<sigrok::Device>(device);
362 *(std::shared_ptr<sigrok::Packet> **) &packet_addr =
363 new std::shared_ptr<sigrok::Packet>(packet);
364 jobject device_obj = env->NewObject(
365 Device, Device_init, device_addr, true);
366 jobject packet_obj = env->NewObject(
367 Packet, Packet_init, packet_addr, true);
368 env->CallVoidMethod(obj_ref, method, device_obj, packet_obj);
369 if (env->ExceptionCheck())
370 throw sigrok::Error(SR_ERR);
377 %define %enumextras(Class)
380 /* Ignore these for now, need fixes. */
381 %ignore sigrok::Context::create_analog_packet;
382 %ignore sigrok::Context::create_meta_packet;
383 %ignore sigrok::Meta::config;
385 %include "bindings/swig/classes.i"