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->CallObjectMethod($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 pre=" $javaclassname temp$javainput = new $javaclassname();
98 for (java.util.Map.Entry<JKey, JValue> entry : $javainput.entrySet())
99 temp$javainput.set(entry.getKey(), entry.getValue());",
100 pgcppname="temp$javainput")
101 std::map< CKey, CValue > "$javaclassname.getCPtr(temp$javainput)"
103 %typemap(javaout) std::map< CKey, CValue > {
104 return (java.util.Map<JKey, JValue>)$jnicall;
109 /* Specialisation for string->string maps. */
111 MAP_COMMON(std::string, std::string, String, String)
113 %typemap(jni) std::map<std::string, std::string>
115 %typemap(jtype) std::map<std::string, std::string>
116 "java.util.Map<String,String>"
118 %typemap(out) std::map<std::string, std::string> {
119 jclass HashMap = jenv->FindClass("java/util/HashMap");
120 jmethodID init = jenv->GetMethodID(HashMap, "<init>", "()V");
121 jmethodID put = jenv->GetMethodID(HashMap, "put",
122 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
123 $result = jenv->NewObject(HashMap, init);
124 for (auto entry : $1)
125 jenv->CallObjectMethod($result, put,
126 jenv->NewStringUTF(entry.first.c_str()),
127 jenv->NewStringUTF(entry.second.c_str()));
130 /* Specialisation macro for string->shared_ptr maps. */
132 %define STRING_TO_SHARED_PTR_MAP(ClassName)
134 %typemap(jni) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
136 %typemap(jtype) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
137 "java.util.Map<String,ClassName>"
139 MAP_COMMON(std::string, std::shared_ptr<sigrok::ClassName>, String, ClassName)
141 %typemap(out) std::map<std::string, std::shared_ptr<sigrok::ClassName> > {
142 jclass HashMap = jenv->FindClass("java/util/HashMap");
143 jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
144 jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
145 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
146 jclass Value = jenv->FindClass("org/sigrok/core/classes/" #ClassName);
147 jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
148 $result = jenv->NewObject(HashMap, HashMap_init);
150 for (auto entry : $1)
152 *(std::shared_ptr< sigrok::ClassName > **)&value =
153 new std::shared_ptr< sigrok::ClassName>(entry.second);
154 jenv->CallObjectMethod($result, HashMap_put,
155 jenv->NewStringUTF(entry.first.c_str()),
156 jenv->NewObject(Value, Value_init, value, true));
162 STRING_TO_SHARED_PTR_MAP(Driver)
163 STRING_TO_SHARED_PTR_MAP(InputFormat)
164 STRING_TO_SHARED_PTR_MAP(OutputFormat)
166 /* Specialisation for ConfigKey->Variant maps */
168 MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
170 %typemap(jni) std::map<const sigrok::ConfigKey, Glib::VariantBase> "jobject"
171 %typemap(jtype) std::map<const sigrok::ConfigKey, Glib::VariantBase>
172 "java.util.Map<ConfigKey,Variant>"
174 %typemap(out) std::map<const sigrok::ConfigKey *, Glib::VariantBase> {
175 jclass HashMap = jenv->FindClass("java/util/HashMap");
176 jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
177 jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
178 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
179 jclass ConfigKey = jenv->FindClass("org/sigrok/core/classes/ConfigKey");
180 jmethodID ConfigKey_init = jenv->GetMethodID(ConfigKey, "<init>", "(JZ)V");
181 jclass Variant = jenv->FindClass("org/sigrok/core/classes/Variant");
182 jmethodID Variant_init = jenv->GetMethodID(Variant, "<init>", "(JZ)V");
183 $result = jenv->NewObject(HashMap, HashMap_init);
186 for (auto entry : $1)
188 *(const sigrok::ConfigKey **) &key = entry.first;
189 *(Glib::VariantBase **) &value = new Glib::VariantBase(entry.second);
190 jenv->CallObjectMethod($result, HashMap_put,
191 jenv->NewObject(ConfigKey, ConfigKey_init, key, false));
192 jenv->NewObject(Variant, Variant_init, value, true));
196 /* Support Driver.scan() with no arguments. */
197 %ignore sigrok::Driver::scan;
199 %extend sigrok::Driver {
200 std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan()
202 std::map<const sigrok::ConfigKey *, Glib::VariantBase> options;
203 return $self->scan(options);
206 std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan(
207 std::map<const sigrok::ConfigKey *, Glib::VariantBase> options)
209 return $self->scan(options);
213 /* Support InputFormat.create_input() with or without options. */
214 %ignore sigrok::InputFormat::create_input;
216 %extend sigrok::InputFormat {
217 std::shared_ptr<sigrok::Input> create_input()
219 return $self->create_input();
222 std::shared_ptr<sigrok::Input> create_input(
223 std::map<std::string, Glib::VariantBase> options)
225 return $self->create_input(options);
229 /* Support OutputFormat.create_output() with or without options. */
230 %ignore sigrok::OutputFormat::create_output;
232 %extend sigrok::OutputFormat {
233 std::shared_ptr<sigrok::Output> create_output(
234 std::shared_ptr<sigrok::Device> device)
236 return $self->create_output(device);
239 std::shared_ptr<sigrok::Output> create_output(
240 std::shared_ptr<sigrok::Device> device,
241 std::map<std::string, Glib::VariantBase> options)
243 return $self->create_output(device, options);
246 std::shared_ptr<sigrok::Output> create_output(
247 std::string filename,
248 std::shared_ptr<sigrok::Device> device)
250 return $self->create_output(filename, device);
253 std::shared_ptr<sigrok::Output> create_output(
254 std::string filename,
255 std::shared_ptr<sigrok::Device> device,
256 std::map<std::string, Glib::VariantBase> options)
258 return $self->create_output(filename, device, options);
262 /* Pass JNIEnv parameter to C++ extension methods requiring it. */
264 %typemap(in, numinputs=0) JNIEnv * %{
268 /* Support Java log callbacks. */
270 %typemap(javaimports) sigrok::Context
271 "import org.sigrok.core.interfaces.LogCallback;"
274 typedef jobject jlogcallback;
277 %typemap(jni) jlogcallback "jlogcallback"
278 %typemap(jtype) jlogcallback "LogCallback"
279 %typemap(jstype) jlogcallback "LogCallback"
280 %typemap(javain) jlogcallback "$javainput"
282 %extend sigrok::Context
284 void add_log_callback(JNIEnv *env, jlogcallback obj)
286 jclass obj_class = env->GetObjectClass(obj);
287 jmethodID method = env->GetMethodID(obj_class, "run",
288 "(Lorg/sigrok/core/classes/LogLevel;Ljava/lang/String;)V");
289 jclass LogLevel = (jclass) env->NewGlobalRef(
290 env->FindClass("org/sigrok/core/classes/LogLevel"));
291 jmethodID LogLevel_init = env->GetMethodID(LogLevel, "<init>", "(JZ)V");
292 jobject obj_ref = env->NewGlobalRef(obj);
294 $self->set_log_callback([=] (
295 const sigrok::LogLevel *loglevel,
299 *(const sigrok::LogLevel **) &loglevel_addr = loglevel;
300 jobject loglevel_obj = env->NewObject(
301 LogLevel, LogLevel_init, loglevel_addr, false);
302 jobject message_obj = env->NewStringUTF(message.c_str());
303 env->CallVoidMethod(obj_ref, method, loglevel_obj, message_obj);
304 if (env->ExceptionCheck())
305 throw sigrok::Error(SR_ERR);
310 /* Support Java datafeed callbacks. */
312 %typemap(javaimports) sigrok::Session
313 "import org.sigrok.core.interfaces.DatafeedCallback;"
316 typedef jobject jdatafeedcallback;
319 %typemap(jni) jdatafeedcallback "jdatafeedcallback"
320 %typemap(jtype) jdatafeedcallback "DatafeedCallback"
321 %typemap(jstype) jdatafeedcallback "DatafeedCallback"
322 %typemap(javain) jdatafeedcallback "$javainput"
324 %extend sigrok::Session
326 void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj)
328 jclass obj_class = env->GetObjectClass(obj);
329 jmethodID method = env->GetMethodID(obj_class, "run",
330 "(Lorg/sigrok/core/classes/Device;Lorg/sigrok/core/classes/Packet;)V");
331 jclass Device = (jclass) env->NewGlobalRef(
332 env->FindClass("org/sigrok/core/classes/Device"));
333 jmethodID Device_init = env->GetMethodID(Device, "<init>", "(JZ)V");
334 jclass Packet = (jclass) env->NewGlobalRef(
335 env->FindClass("org/sigrok/core/classes/Packet"));
336 jmethodID Packet_init = env->GetMethodID(Packet, "<init>", "(JZ)V");
337 jobject obj_ref = env->NewGlobalRef(obj);
339 $self->add_datafeed_callback([=] (
340 std::shared_ptr<sigrok::Device> device,
341 std::shared_ptr<sigrok::Packet> packet)
345 *(std::shared_ptr<sigrok::Device> **) &device_addr =
346 new std::shared_ptr<sigrok::Device>(device);
347 *(std::shared_ptr<sigrok::Packet> **) &packet_addr =
348 new std::shared_ptr<sigrok::Packet>(packet);
349 jobject device_obj = env->NewObject(
350 Device, Device_init, device_addr, true);
351 jobject packet_obj = env->NewObject(
352 Packet, Packet_init, packet_addr, true);
353 env->CallVoidMethod(obj_ref, method, device_obj, packet_obj);
354 if (env->ExceptionCheck())
355 throw sigrok::Error(SR_ERR);
362 %define %attributevector(Class, Type, Name, Get)
363 %attributeval(sigrok::Class, Type, Name, Get);
366 %define %attributemap(Class, Type, Name, Get)
367 %attributeval(sigrok::Class, Type, Name, Get);
370 %define %enumextras(Class)
373 /* Ignore these for now, need fixes. */
374 %ignore sigrok::Context::create_analog_packet;
375 %ignore sigrok::Context::create_meta_packet;
377 %include "bindings/swig/classes.i"