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 = $javaclassname.convertVector($javainput);",
58 pgcppname="temp$javainput")
59 std::vector< CValue > "$javaclassname.getCPtr(temp$javainput)"
61 %typemap(javacode) std::vector< CValue > %{
62 static $javaclassname convertVector(java.util.Vector<JValue> in)
64 $javaclassname out = new $javaclassname();
65 for (JValue value : in)
71 %typemap(javaout) std::vector< CValue > {
72 return (java.util.Vector<JValue>)$jnicall;
75 %typemap(out) std::vector< CValue > {
76 jclass Vector = jenv->FindClass("java/util/Vector");
77 jmethodID Vector_init = jenv->GetMethodID(Vector, "<init>", "()V");
78 jmethodID Vector_add = jenv->GetMethodID(Vector, "add",
79 "(Ljava/lang/Object;)Z");
80 jclass Value = jenv->FindClass("org/sigrok/core/classes/" #JValue);
81 jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
82 $result = jenv->NewObject(Vector, Vector_init);
86 *(CValue **) &value = new CValue(entry);
87 jenv->CallObjectMethod($result, Vector_add,
88 jenv->NewObject(Value, Value_init, value, true));
94 VECTOR(std::shared_ptr<sigrok::Channel>, Channel)
95 VECTOR(std::shared_ptr<sigrok::HardwareDevice>, HardwareDevice)
97 /* Common macro for mapping between std::map and java.util.Map */
99 %define MAP_COMMON(CKey, CValue, JKey, JValue)
101 %typemap(jstype) std::map< CKey, CValue >
102 "java.util.Map<JKey, JValue>"
105 pre=" $javaclassname temp$javainput = $javaclassname.convertMap($javainput);",
106 pgcppname="temp$javainput")
107 std::map< CKey, CValue > "$javaclassname.getCPtr(temp$javainput)"
109 %typemap(javacode) std::map< CKey, CValue > %{
110 static $javaclassname convertMap(java.util.Map<JKey,JValue> in)
112 $javaclassname out = new $javaclassname();
113 for (java.util.Map.Entry<JKey, JValue> entry : in.entrySet())
114 out.set(entry.getKey(), entry.getValue());
119 %typemap(javaout) std::map< CKey, CValue > {
120 return (java.util.Map<JKey, JValue>)$jnicall;
125 /* Specialisation for string->string maps. */
127 MAP_COMMON(std::string, std::string, String, String)
129 %typemap(jni) std::map<std::string, std::string>
131 %typemap(jtype) std::map<std::string, std::string>
132 "java.util.Map<String,String>"
134 %typemap(out) std::map<std::string, std::string> {
135 jclass HashMap = jenv->FindClass("java/util/HashMap");
136 jmethodID init = jenv->GetMethodID(HashMap, "<init>", "()V");
137 jmethodID put = jenv->GetMethodID(HashMap, "put",
138 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
139 $result = jenv->NewObject(HashMap, init);
140 for (auto entry : $1)
141 jenv->CallObjectMethod($result, put,
142 jenv->NewStringUTF(entry.first.c_str()),
143 jenv->NewStringUTF(entry.second.c_str()));
146 /* Specialisation macro for string->shared_ptr maps. */
148 %define STRING_TO_SHARED_PTR_MAP(ClassName)
150 %typemap(jni) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
152 %typemap(jtype) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
153 "java.util.Map<String,ClassName>"
155 MAP_COMMON(std::string, std::shared_ptr<sigrok::ClassName>, String, ClassName)
157 %typemap(out) std::map<std::string, std::shared_ptr<sigrok::ClassName> > {
158 jclass HashMap = jenv->FindClass("java/util/HashMap");
159 jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
160 jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
161 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
162 jclass Value = jenv->FindClass("org/sigrok/core/classes/" #ClassName);
163 jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
164 $result = jenv->NewObject(HashMap, HashMap_init);
166 for (auto entry : $1)
168 *(std::shared_ptr< sigrok::ClassName > **)&value =
169 new std::shared_ptr< sigrok::ClassName>(entry.second);
170 jenv->CallObjectMethod($result, HashMap_put,
171 jenv->NewStringUTF(entry.first.c_str()),
172 jenv->NewObject(Value, Value_init, value, true));
178 STRING_TO_SHARED_PTR_MAP(Driver)
179 STRING_TO_SHARED_PTR_MAP(InputFormat)
180 STRING_TO_SHARED_PTR_MAP(OutputFormat)
182 /* Specialisation for ConfigKey->Variant maps */
184 MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
186 %typemap(jni) std::map<const sigrok::ConfigKey, Glib::VariantBase> "jobject"
187 %typemap(jtype) std::map<const sigrok::ConfigKey, Glib::VariantBase>
188 "java.util.Map<ConfigKey,Variant>"
190 %typemap(out) std::map<const sigrok::ConfigKey *, Glib::VariantBase> {
191 jclass HashMap = jenv->FindClass("java/util/HashMap");
192 jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
193 jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
194 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
195 jclass ConfigKey = jenv->FindClass("org/sigrok/core/classes/ConfigKey");
196 jmethodID ConfigKey_init = jenv->GetMethodID(ConfigKey, "<init>", "(JZ)V");
197 jclass Variant = jenv->FindClass("org/sigrok/core/classes/Variant");
198 jmethodID Variant_init = jenv->GetMethodID(Variant, "<init>", "(JZ)V");
199 $result = jenv->NewObject(HashMap, HashMap_init);
202 for (auto entry : $1)
204 *(const sigrok::ConfigKey **) &key = entry.first;
205 *(Glib::VariantBase **) &value = new Glib::VariantBase(entry.second);
206 jenv->CallObjectMethod($result, HashMap_put,
207 jenv->NewObject(ConfigKey, ConfigKey_init, key, false));
208 jenv->NewObject(Variant, Variant_init, value, true));
212 /* Support Driver.scan() with no arguments. */
213 %ignore sigrok::Driver::scan;
215 %extend sigrok::Driver {
216 std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan()
218 std::map<const sigrok::ConfigKey *, Glib::VariantBase> options;
219 return $self->scan(options);
222 std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan(
223 std::map<const sigrok::ConfigKey *, Glib::VariantBase> options)
225 return $self->scan(options);
229 /* Support InputFormat.create_input() with or without options. */
230 %ignore sigrok::InputFormat::create_input;
232 %extend sigrok::InputFormat {
233 std::shared_ptr<sigrok::Input> create_input()
235 return $self->create_input();
238 std::shared_ptr<sigrok::Input> create_input(
239 std::map<std::string, Glib::VariantBase> options)
241 return $self->create_input(options);
245 /* Support OutputFormat.create_output() with or without options. */
246 %ignore sigrok::OutputFormat::create_output;
248 %extend sigrok::OutputFormat {
249 std::shared_ptr<sigrok::Output> create_output(
250 std::shared_ptr<sigrok::Device> device)
252 return $self->create_output(device);
255 std::shared_ptr<sigrok::Output> create_output(
256 std::shared_ptr<sigrok::Device> device,
257 std::map<std::string, Glib::VariantBase> options)
259 return $self->create_output(device, options);
262 std::shared_ptr<sigrok::Output> create_output(
263 std::string filename,
264 std::shared_ptr<sigrok::Device> device)
266 return $self->create_output(filename, device);
269 std::shared_ptr<sigrok::Output> create_output(
270 std::string filename,
271 std::shared_ptr<sigrok::Device> device,
272 std::map<std::string, Glib::VariantBase> options)
274 return $self->create_output(filename, device, options);
278 /* Pass JNIEnv parameter to C++ extension methods requiring it. */
280 %typemap(in, numinputs=0) JNIEnv * %{
284 /* Support Java log callbacks. */
286 %typemap(javaimports) sigrok::Context
287 "import org.sigrok.core.interfaces.LogCallback;"
290 typedef jobject jlogcallback;
293 %typemap(jni) jlogcallback "jlogcallback"
294 %typemap(jtype) jlogcallback "LogCallback"
295 %typemap(jstype) jlogcallback "LogCallback"
296 %typemap(javain) jlogcallback "$javainput"
298 %extend sigrok::Context
300 void add_log_callback(JNIEnv *env, jlogcallback obj)
302 jclass obj_class = env->GetObjectClass(obj);
303 jmethodID method = env->GetMethodID(obj_class, "run",
304 "(Lorg/sigrok/core/classes/LogLevel;Ljava/lang/String;)V");
305 jclass LogLevel = (jclass) env->NewGlobalRef(
306 env->FindClass("org/sigrok/core/classes/LogLevel"));
307 jmethodID LogLevel_init = env->GetMethodID(LogLevel, "<init>", "(JZ)V");
308 jobject obj_ref = env->NewGlobalRef(obj);
310 $self->set_log_callback([=] (
311 const sigrok::LogLevel *loglevel,
315 *(const sigrok::LogLevel **) &loglevel_addr = loglevel;
316 jobject loglevel_obj = env->NewObject(
317 LogLevel, LogLevel_init, loglevel_addr, false);
318 jobject message_obj = env->NewStringUTF(message.c_str());
319 env->CallVoidMethod(obj_ref, method, loglevel_obj, message_obj);
320 if (env->ExceptionCheck())
321 throw sigrok::Error(SR_ERR);
326 /* Support Java datafeed callbacks. */
328 %typemap(javaimports) sigrok::Session
329 "import org.sigrok.core.interfaces.DatafeedCallback;"
332 typedef jobject jdatafeedcallback;
335 %typemap(jni) jdatafeedcallback "jdatafeedcallback"
336 %typemap(jtype) jdatafeedcallback "DatafeedCallback"
337 %typemap(jstype) jdatafeedcallback "DatafeedCallback"
338 %typemap(javain) jdatafeedcallback "$javainput"
340 %extend sigrok::Session
342 void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj)
344 jclass obj_class = env->GetObjectClass(obj);
345 jmethodID method = env->GetMethodID(obj_class, "run",
346 "(Lorg/sigrok/core/classes/Device;Lorg/sigrok/core/classes/Packet;)V");
347 jclass Device = (jclass) env->NewGlobalRef(
348 env->FindClass("org/sigrok/core/classes/Device"));
349 jmethodID Device_init = env->GetMethodID(Device, "<init>", "(JZ)V");
350 jclass Packet = (jclass) env->NewGlobalRef(
351 env->FindClass("org/sigrok/core/classes/Packet"));
352 jmethodID Packet_init = env->GetMethodID(Packet, "<init>", "(JZ)V");
353 jobject obj_ref = env->NewGlobalRef(obj);
355 $self->add_datafeed_callback([=] (
356 std::shared_ptr<sigrok::Device> device,
357 std::shared_ptr<sigrok::Packet> packet)
361 *(std::shared_ptr<sigrok::Device> **) &device_addr =
362 new std::shared_ptr<sigrok::Device>(device);
363 *(std::shared_ptr<sigrok::Packet> **) &packet_addr =
364 new std::shared_ptr<sigrok::Packet>(packet);
365 jobject device_obj = env->NewObject(
366 Device, Device_init, device_addr, true);
367 jobject packet_obj = env->NewObject(
368 Packet, Packet_init, packet_addr, true);
369 env->CallVoidMethod(obj_ref, method, device_obj, packet_obj);
370 if (env->ExceptionCheck())
371 throw sigrok::Error(SR_ERR);
378 %define %attributevector(Class, Type, Name, Get)
379 %attributeval(sigrok::Class, Type, Name, Get);
382 %define %attributemap(Class, Type, Name, Get)
383 %attributeval(sigrok::Class, Type, Name, Get);
386 %define %enumextras(Class)
389 /* Ignore these for now, need fixes. */
390 %ignore sigrok::Context::create_analog_packet;
391 %ignore sigrok::Context::create_meta_packet;
393 %include "bindings/swig/classes.i"