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 /* Map between std::vector and java.util.Vector */
48 %define VECTOR(CValue, JValue)
50 %typemap(jni) std::vector< CValue > "jobject"
51 %typemap(jtype) std::vector< CValue > "java.util.Vector<JValue>"
52 %typemap(jstype) std::vector< CValue > "java.util.Vector<JValue>"
55 pre=" $javaclassname temp$javainput = $javaclassname.convertVector($javainput);",
56 pgcppname="temp$javainput")
57 std::vector< CValue > "$javaclassname.getCPtr(temp$javainput)"
59 %typemap(javacode) std::vector< CValue > %{
60 static $javaclassname convertVector(java.util.Vector<JValue> in)
62 $javaclassname out = new $javaclassname();
63 for (JValue value : in)
69 %typemap(javaout) std::vector< CValue > {
70 return (java.util.Vector<JValue>)$jnicall;
73 %typemap(out) std::vector< CValue > {
74 jclass Vector = jenv->FindClass("java/util/Vector");
75 jmethodID Vector_init = jenv->GetMethodID(Vector, "<init>", "()V");
76 jmethodID Vector_add = jenv->GetMethodID(Vector, "add",
77 "(Ljava/lang/Object;)Z");
78 jclass Value = jenv->FindClass("org/sigrok/core/classes/" #JValue);
79 jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
80 $result = jenv->NewObject(Vector, Vector_init);
84 *(CValue **) &value = new CValue(entry);
85 jenv->CallObjectMethod($result, Vector_add,
86 jenv->NewObject(Value, Value_init, value, true));
92 VECTOR(std::shared_ptr<sigrok::Channel>, Channel)
93 VECTOR(std::shared_ptr<sigrok::HardwareDevice>, HardwareDevice)
95 /* Common macro for mapping between std::map and java.util.Map */
97 %define MAP_COMMON(CKey, CValue, JKey, JValue)
99 %typemap(jstype) std::map< CKey, CValue >
100 "java.util.Map<JKey, JValue>"
103 pre=" $javaclassname temp$javainput = $javaclassname.convertMap($javainput);",
104 pgcppname="temp$javainput")
105 std::map< CKey, CValue > "$javaclassname.getCPtr(temp$javainput)"
107 %typemap(javacode) std::map< CKey, CValue > %{
108 static $javaclassname convertMap(java.util.Map<JKey,JValue> in)
110 $javaclassname out = new $javaclassname();
111 for (java.util.Map.Entry<JKey, JValue> entry : in.entrySet())
112 out.set(entry.getKey(), entry.getValue());
117 %typemap(javaout) std::map< CKey, CValue > {
118 return (java.util.Map<JKey, JValue>)$jnicall;
123 /* Specialisation for string->string maps. */
125 MAP_COMMON(std::string, std::string, String, String)
127 %typemap(jni) std::map<std::string, std::string>
129 %typemap(jtype) std::map<std::string, std::string>
130 "java.util.Map<String,String>"
132 %typemap(out) std::map<std::string, std::string> {
133 jclass HashMap = jenv->FindClass("java/util/HashMap");
134 jmethodID init = jenv->GetMethodID(HashMap, "<init>", "()V");
135 jmethodID put = jenv->GetMethodID(HashMap, "put",
136 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
137 $result = jenv->NewObject(HashMap, init);
138 for (auto entry : $1)
139 jenv->CallObjectMethod($result, put,
140 jenv->NewStringUTF(entry.first.c_str()),
141 jenv->NewStringUTF(entry.second.c_str()));
144 /* Specialisation macro for string->shared_ptr maps. */
146 %define STRING_TO_SHARED_PTR_MAP(ClassName)
148 %typemap(jni) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
150 %typemap(jtype) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
151 "java.util.Map<String,ClassName>"
153 MAP_COMMON(std::string, std::shared_ptr<sigrok::ClassName>, String, ClassName)
155 %typemap(out) std::map<std::string, std::shared_ptr<sigrok::ClassName> > {
156 jclass HashMap = jenv->FindClass("java/util/HashMap");
157 jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
158 jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
159 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
160 jclass Value = jenv->FindClass("org/sigrok/core/classes/" #ClassName);
161 jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
162 $result = jenv->NewObject(HashMap, HashMap_init);
164 for (auto entry : $1)
166 *(std::shared_ptr< sigrok::ClassName > **)&value =
167 new std::shared_ptr< sigrok::ClassName>(entry.second);
168 jenv->CallObjectMethod($result, HashMap_put,
169 jenv->NewStringUTF(entry.first.c_str()),
170 jenv->NewObject(Value, Value_init, value, true));
176 STRING_TO_SHARED_PTR_MAP(Driver)
177 STRING_TO_SHARED_PTR_MAP(InputFormat)
178 STRING_TO_SHARED_PTR_MAP(OutputFormat)
180 /* Specialisation for ConfigKey->Variant maps */
182 MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
184 %typemap(jni) std::map<const sigrok::ConfigKey, Glib::VariantBase> "jobject"
185 %typemap(jtype) std::map<const sigrok::ConfigKey, Glib::VariantBase>
186 "java.util.Map<ConfigKey,Variant>"
188 %typemap(out) std::map<const sigrok::ConfigKey *, Glib::VariantBase> {
189 jclass HashMap = jenv->FindClass("java/util/HashMap");
190 jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
191 jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
192 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
193 jclass ConfigKey = jenv->FindClass("org/sigrok/core/classes/ConfigKey");
194 jmethodID ConfigKey_init = jenv->GetMethodID(ConfigKey, "<init>", "(JZ)V");
195 jclass Variant = jenv->FindClass("org/sigrok/core/classes/Variant");
196 jmethodID Variant_init = jenv->GetMethodID(Variant, "<init>", "(JZ)V");
197 $result = jenv->NewObject(HashMap, HashMap_init);
200 for (auto entry : $1)
202 *(const sigrok::ConfigKey **) &key = entry.first;
203 *(Glib::VariantBase **) &value = new Glib::VariantBase(entry.second);
204 jenv->CallObjectMethod($result, HashMap_put,
205 jenv->NewObject(ConfigKey, ConfigKey_init, key, false));
206 jenv->NewObject(Variant, Variant_init, value, true));
210 /* Support Driver.scan() with no arguments. */
211 %ignore sigrok::Driver::scan;
213 %extend sigrok::Driver {
214 std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan()
216 std::map<const sigrok::ConfigKey *, Glib::VariantBase> options;
217 return $self->scan(options);
220 std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan(
221 std::map<const sigrok::ConfigKey *, Glib::VariantBase> options)
223 return $self->scan(options);
227 /* Support InputFormat.create_input() with or without options. */
228 %ignore sigrok::InputFormat::create_input;
230 %extend sigrok::InputFormat {
231 std::shared_ptr<sigrok::Input> create_input()
233 return $self->create_input();
236 std::shared_ptr<sigrok::Input> create_input(
237 std::map<std::string, Glib::VariantBase> options)
239 return $self->create_input(options);
243 /* Support OutputFormat.create_output() with or without options. */
244 %ignore sigrok::OutputFormat::create_output;
246 %extend sigrok::OutputFormat {
247 std::shared_ptr<sigrok::Output> create_output(
248 std::shared_ptr<sigrok::Device> device)
250 return $self->create_output(device);
253 std::shared_ptr<sigrok::Output> create_output(
254 std::shared_ptr<sigrok::Device> device,
255 std::map<std::string, Glib::VariantBase> options)
257 return $self->create_output(device, options);
260 std::shared_ptr<sigrok::Output> create_output(
261 std::string filename,
262 std::shared_ptr<sigrok::Device> device)
264 return $self->create_output(filename, device);
267 std::shared_ptr<sigrok::Output> create_output(
268 std::string filename,
269 std::shared_ptr<sigrok::Device> device,
270 std::map<std::string, Glib::VariantBase> options)
272 return $self->create_output(filename, device, options);
276 /* Pass JNIEnv parameter to C++ extension methods requiring it. */
278 %typemap(in, numinputs=0) JNIEnv * %{
282 /* Support Java log callbacks. */
284 %typemap(javaimports) sigrok::Context
285 "import org.sigrok.core.interfaces.LogCallback;"
288 typedef jobject jlogcallback;
291 %typemap(jni) jlogcallback "jlogcallback"
292 %typemap(jtype) jlogcallback "LogCallback"
293 %typemap(jstype) jlogcallback "LogCallback"
294 %typemap(javain) jlogcallback "$javainput"
296 %extend sigrok::Context
298 void add_log_callback(JNIEnv *env, jlogcallback obj)
300 jclass obj_class = env->GetObjectClass(obj);
301 jmethodID method = env->GetMethodID(obj_class, "run",
302 "(Lorg/sigrok/core/classes/LogLevel;Ljava/lang/String;)V");
303 jclass LogLevel = (jclass) env->NewGlobalRef(
304 env->FindClass("org/sigrok/core/classes/LogLevel"));
305 jmethodID LogLevel_init = env->GetMethodID(LogLevel, "<init>", "(JZ)V");
306 jobject obj_ref = env->NewGlobalRef(obj);
308 $self->set_log_callback([=] (
309 const sigrok::LogLevel *loglevel,
313 *(const sigrok::LogLevel **) &loglevel_addr = loglevel;
314 jobject loglevel_obj = env->NewObject(
315 LogLevel, LogLevel_init, loglevel_addr, false);
316 jobject message_obj = env->NewStringUTF(message.c_str());
317 env->CallVoidMethod(obj_ref, method, loglevel_obj, message_obj);
318 if (env->ExceptionCheck())
319 throw sigrok::Error(SR_ERR);
324 /* Support Java datafeed callbacks. */
326 %typemap(javaimports) sigrok::Session
327 "import org.sigrok.core.interfaces.DatafeedCallback;"
330 typedef jobject jdatafeedcallback;
333 %typemap(jni) jdatafeedcallback "jdatafeedcallback"
334 %typemap(jtype) jdatafeedcallback "DatafeedCallback"
335 %typemap(jstype) jdatafeedcallback "DatafeedCallback"
336 %typemap(javain) jdatafeedcallback "$javainput"
338 %extend sigrok::Session
340 void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj)
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 jclass Device = (jclass) env->NewGlobalRef(
346 env->FindClass("org/sigrok/core/classes/Device"));
347 jmethodID Device_init = env->GetMethodID(Device, "<init>", "(JZ)V");
348 jclass Packet = (jclass) env->NewGlobalRef(
349 env->FindClass("org/sigrok/core/classes/Packet"));
350 jmethodID Packet_init = env->GetMethodID(Packet, "<init>", "(JZ)V");
351 jobject obj_ref = env->NewGlobalRef(obj);
353 $self->add_datafeed_callback([=] (
354 std::shared_ptr<sigrok::Device> device,
355 std::shared_ptr<sigrok::Packet> packet)
359 *(std::shared_ptr<sigrok::Device> **) &device_addr =
360 new std::shared_ptr<sigrok::Device>(device);
361 *(std::shared_ptr<sigrok::Packet> **) &packet_addr =
362 new std::shared_ptr<sigrok::Packet>(packet);
363 jobject device_obj = env->NewObject(
364 Device, Device_init, device_addr, true);
365 jobject packet_obj = env->NewObject(
366 Packet, Packet_init, packet_addr, true);
367 env->CallVoidMethod(obj_ref, method, device_obj, packet_obj);
368 if (env->ExceptionCheck())
369 throw sigrok::Error(SR_ERR);
376 %define %attributevector(Class, Type, Name, Get)
377 %attributeval(sigrok::Class, Type, Name, Get);
380 %define %attributemap(Class, Type, Name, Get)
381 %attributeval(sigrok::Class, Type, Name, Get);
384 %define %enumextras(Class)
387 /* Ignore this for now, needs a fix. */
388 %ignore sigrok::Context::create_analog_packet;
390 %include "bindings/swig/classes.i"