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;
39 import org.sigrok.core.interfaces.SourceCallback;
42 /* Map Java FileDescriptor objects to int fds */
43 %typemap(jni) int fd "jobject"
44 %typemap(jtype) int fd "java.io.FileDescriptor"
45 %typemap(jstype) int fd "java.io.FileDescriptor"
46 %typemap(javain) int fd "$javainput"
49 jclass FileDescriptor = jenv->FindClass("java/io/FileDescriptor");
50 jfieldID fd = jenv->GetFieldID(FileDescriptor, "fd", "I");
51 $1 = jenv->GetIntField($input, fd);
54 /* Map Glib::VariantBase to a Variant class in Java */
55 %rename(Variant) VariantBase;
60 /* Map between std::vector and java.util.Vector */
61 %define VECTOR(CValue, JValue)
63 %typemap(jni) std::vector< CValue > "jobject"
64 %typemap(jtype) std::vector< CValue > "java.util.Vector<JValue>"
65 %typemap(jstype) std::vector< CValue > "java.util.Vector<JValue>"
68 pre=" $javaclassname temp$javainput = $javaclassname.convertVector($javainput);",
69 pgcppname="temp$javainput")
70 std::vector< CValue > "$javaclassname.getCPtr(temp$javainput)"
72 %typemap(javacode) std::vector< CValue > %{
73 static $javaclassname convertVector(java.util.Vector<JValue> in)
75 $javaclassname out = new $javaclassname();
76 for (JValue value : in)
82 %typemap(javaout) std::vector< CValue > {
83 return (java.util.Vector<JValue>)$jnicall;
86 %typemap(out) std::vector< CValue > {
87 jclass Vector = jenv->FindClass("java/util/Vector");
88 jmethodID Vector_init = jenv->GetMethodID(Vector, "<init>", "()V");
89 jmethodID Vector_add = jenv->GetMethodID(Vector, "add",
90 "(Ljava/lang/Object;)Z");
91 jclass Value = jenv->FindClass("org/sigrok/core/classes/" #JValue);
92 jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
93 $result = jenv->NewObject(Vector, Vector_init);
97 *(CValue **) &value = new CValue(entry);
98 jenv->CallObjectMethod($result, Vector_add,
99 jenv->NewObject(Value, Value_init, value, true));
105 VECTOR(std::shared_ptr<sigrok::Channel>, Channel)
106 VECTOR(std::shared_ptr<sigrok::HardwareDevice>, HardwareDevice)
108 /* Common macro for mapping between std::map and java.util.Map */
110 %define MAP_COMMON(CKey, CValue, JKey, JValue)
112 %typemap(jstype) std::map< CKey, CValue >
113 "java.util.Map<JKey, JValue>"
116 pre=" $javaclassname temp$javainput = $javaclassname.convertMap($javainput);",
117 pgcppname="temp$javainput")
118 std::map< CKey, CValue > "$javaclassname.getCPtr(temp$javainput)"
120 %typemap(javacode) std::map< CKey, CValue > %{
121 static $javaclassname convertMap(java.util.Map<JKey,JValue> in)
123 $javaclassname out = new $javaclassname();
124 for (java.util.Map.Entry<JKey, JValue> entry : in.entrySet())
125 out.set(entry.getKey(), entry.getValue());
130 %typemap(javaout) std::map< CKey, CValue > {
131 return (java.util.Map<JKey, JValue>)$jnicall;
136 /* Specialisation for string->string maps. */
138 MAP_COMMON(std::string, std::string, String, String)
140 %typemap(jni) std::map<std::string, std::string>
142 %typemap(jtype) std::map<std::string, std::string>
143 "java.util.Map<String,String>"
145 %typemap(out) std::map<std::string, std::string> {
146 jclass HashMap = jenv->FindClass("java/util/HashMap");
147 jmethodID init = jenv->GetMethodID(HashMap, "<init>", "()V");
148 jmethodID put = jenv->GetMethodID(HashMap, "put",
149 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
150 $result = jenv->NewObject(HashMap, init);
151 for (auto entry : $1)
152 jenv->CallObjectMethod($result, put,
153 jenv->NewStringUTF(entry.first.c_str()),
154 jenv->NewStringUTF(entry.second.c_str()));
157 /* Specialisation macro for string->shared_ptr maps. */
159 %define STRING_TO_SHARED_PTR_MAP(ClassName)
161 %typemap(jni) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
163 %typemap(jtype) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
164 "java.util.Map<String,ClassName>"
166 MAP_COMMON(std::string, std::shared_ptr<sigrok::ClassName>, String, ClassName)
168 %typemap(out) std::map<std::string, std::shared_ptr<sigrok::ClassName> > {
169 jclass HashMap = jenv->FindClass("java/util/HashMap");
170 jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
171 jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
172 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
173 jclass Value = jenv->FindClass("org/sigrok/core/classes/" #ClassName);
174 jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
175 $result = jenv->NewObject(HashMap, HashMap_init);
177 for (auto entry : $1)
179 *(std::shared_ptr< sigrok::ClassName > **)&value =
180 new std::shared_ptr< sigrok::ClassName>(entry.second);
181 jenv->CallObjectMethod($result, HashMap_put,
182 jenv->NewStringUTF(entry.first.c_str()),
183 jenv->NewObject(Value, Value_init, value, true));
189 STRING_TO_SHARED_PTR_MAP(Driver)
190 STRING_TO_SHARED_PTR_MAP(InputFormat)
191 STRING_TO_SHARED_PTR_MAP(OutputFormat)
193 /* Specialisation for ConfigKey->Variant maps */
195 MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
197 %typemap(jni) std::map<const sigrok::ConfigKey, Glib::VariantBase> "jobject"
198 %typemap(jtype) std::map<const sigrok::ConfigKey, Glib::VariantBase>
199 "java.util.Map<ConfigKey,Variant>"
201 %typemap(out) std::map<const sigrok::ConfigKey *, Glib::VariantBase> {
202 jclass HashMap = jenv->FindClass("java/util/HashMap");
203 jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
204 jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
205 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
206 jclass ConfigKey = jenv->FindClass("org/sigrok/core/classes/ConfigKey");
207 jmethodID ConfigKey_init = jenv->GetMethodID(ConfigKey, "<init>", "(JZ)V");
208 jclass Variant = jenv->FindClass("org/sigrok/core/classes/Variant");
209 jmethodID Variant_init = jenv->GetMethodID(Variant, "<init>", "(JZ)V");
210 $result = jenv->NewObject(HashMap, HashMap_init);
213 for (auto entry : $1)
215 *(const sigrok::ConfigKey **) &key = entry.first;
216 *(Glib::VariantBase **) &value = new Glib::VariantBase(entry.second);
217 jenv->CallObjectMethod($result, HashMap_put,
218 jenv->NewObject(ConfigKey, ConfigKey_init, key, false));
219 jenv->NewObject(Variant, Variant_init, value, true));
223 /* Support Driver.scan() with no arguments. */
224 %extend sigrok::Driver {
225 std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan()
227 std::map<const sigrok::ConfigKey *, Glib::VariantBase> options;
228 return $self->scan(options);
232 /* Support InputFormat.create_input() with no options. */
233 %extend sigrok::InputFormat {
234 std::shared_ptr<sigrok::Input> create_input()
236 std::map<std::string, Glib::VariantBase> options;
237 return $self->create_input(options);
241 /* Support OutputFormat.create_output(device) with no options. */
242 %extend sigrok::OutputFormat {
243 std::shared_ptr<sigrok::Output> create_output(
244 std::shared_ptr<sigrok::Device> device)
246 std::map<std::string, Glib::VariantBase> options;
247 return $self->create_output(device, options);
251 /* Pass JNIEnv parameter to C++ extension methods requiring it. */
253 %typemap(in, numinputs=0) JNIEnv * %{
257 /* Support Java log callbacks. */
259 %typemap(javaimports) sigrok::Context
260 "import org.sigrok.core.interfaces.LogCallback;"
263 typedef jobject jlogcallback;
266 %typemap(jni) jlogcallback "jlogcallback"
267 %typemap(jtype) jlogcallback "LogCallback"
268 %typemap(jstype) jlogcallback "LogCallback"
269 %typemap(javain) jlogcallback "$javainput"
271 %extend sigrok::Context
273 void add_log_callback(JNIEnv *env, jlogcallback obj)
275 jclass obj_class = env->GetObjectClass(obj);
276 jmethodID method = env->GetMethodID(obj_class, "run",
277 "(Lorg/sigrok/core/classes/LogLevel;Ljava/lang/String;)V");
278 jclass LogLevel = (jclass) env->NewGlobalRef(
279 env->FindClass("org/sigrok/core/classes/LogLevel"));
280 jmethodID LogLevel_init = env->GetMethodID(LogLevel, "<init>", "(JZ)V");
281 jobject obj_ref = env->NewGlobalRef(obj);
283 $self->set_log_callback([=] (
284 const sigrok::LogLevel *loglevel,
288 *(const sigrok::LogLevel **) &loglevel_addr = loglevel;
289 jobject loglevel_obj = env->NewObject(
290 LogLevel, LogLevel_init, loglevel_addr, false);
291 jobject message_obj = env->NewStringUTF(message.c_str());
292 env->CallVoidMethod(obj_ref, method, loglevel_obj, message_obj);
293 if (env->ExceptionCheck())
294 throw sigrok::Error(SR_ERR);
299 /* Support Java datafeed callbacks. */
301 %typemap(javaimports) sigrok::Session
302 "import org.sigrok.core.interfaces.DatafeedCallback;"
305 typedef jobject jdatafeedcallback;
308 %typemap(jni) jdatafeedcallback "jdatafeedcallback"
309 %typemap(jtype) jdatafeedcallback "DatafeedCallback"
310 %typemap(jstype) jdatafeedcallback "DatafeedCallback"
311 %typemap(javain) jdatafeedcallback "$javainput"
313 %extend sigrok::Session
315 void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj)
317 jclass obj_class = env->GetObjectClass(obj);
318 jmethodID method = env->GetMethodID(obj_class, "run",
319 "(Lorg/sigrok/core/classes/Device;Lorg/sigrok/core/classes/Packet;)V");
320 jclass Device = (jclass) env->NewGlobalRef(
321 env->FindClass("org/sigrok/core/classes/Device"));
322 jmethodID Device_init = env->GetMethodID(Device, "<init>", "(JZ)V");
323 jclass Packet = (jclass) env->NewGlobalRef(
324 env->FindClass("org/sigrok/core/classes/Packet"));
325 jmethodID Packet_init = env->GetMethodID(Packet, "<init>", "(JZ)V");
326 jobject obj_ref = env->NewGlobalRef(obj);
328 $self->add_datafeed_callback([=] (
329 std::shared_ptr<sigrok::Device> device,
330 std::shared_ptr<sigrok::Packet> packet)
334 *(std::shared_ptr<sigrok::Device> **) &device_addr =
335 new std::shared_ptr<sigrok::Device>(device);
336 *(std::shared_ptr<sigrok::Packet> **) &packet_addr =
337 new std::shared_ptr<sigrok::Packet>(packet);
338 jobject device_obj = env->NewObject(
339 Device, Device_init, device_addr, true);
340 jobject packet_obj = env->NewObject(
341 Packet, Packet_init, packet_addr, true);
342 env->CallVoidMethod(obj_ref, method, device_obj, packet_obj);
343 if (env->ExceptionCheck())
344 throw sigrok::Error(SR_ERR);
349 /* Support Java event source callbacks. */
351 %typemap(javaimports) sigrok::EventSource
352 "import org.sigrok.core.interfaces.SourceCallback;"
355 typedef jobject jsourcecallback;
358 %typemap(jni) jsourcecallback "jsourcecallback"
359 %typemap(jtype) jsourcecallback "SourceCallback"
360 %typemap(jstype) jsourcecallback "SourceCallback"
361 %typemap(javain) jsourcecallback "$javainput"
363 %extend sigrok::EventSource
365 std::shared_ptr<sigrok::EventSource> create(
366 int fd, Glib::IOCondition events, int timeout,
367 JNIEnv *env, jsourcecallback obj)
370 jclass obj_class = env->GetObjectClass(obj);
371 jmethodID method = env->GetMethodID(obj_class, "run", "(I)V");
372 jobject obj_ref = env->NewGlobalRef(obj);
374 return sigrok::EventSource::create(fd, events, timeout, [=] (int revents)
376 bool result = env->CallBooleanMethod(obj_ref, method, revents);
377 if (env->ExceptionCheck())
378 throw sigrok::Error(SR_ERR);
386 %define %attributevector(Class, Type, Name, Get)
387 %attributeval(sigrok::Class, Type, Name, Get);
390 %define %attributemap(Class, Type, Name, Get)
391 %attributeval(sigrok::Class, Type, Name, Get);
394 %define %enumextras(Class)
397 /* Ignore this for now, needs a fix. */
398 %ignore sigrok::Context::create_analog_packet;
400 %include "bindings/swig/classes.i"