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 sigrok++ 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(out) std::map<std::string, std::string> {
141 jclass HashMap = jenv->FindClass("java/util/HashMap");
142 jmethodID init = jenv->GetMethodID(HashMap, "<init>", "()V");
143 jmethodID put = jenv->GetMethodID(HashMap, "put",
144 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
145 $result = jenv->NewObject(HashMap, init);
146 for (auto entry : $1)
147 jenv->CallObjectMethod($result, put,
148 jenv->NewStringUTF(entry.first.c_str()),
149 jenv->NewStringUTF(entry.second.c_str()));
152 /* Specialisation macro for string->shared_ptr maps. */
154 %define STRING_TO_SHARED_PTR_MAP(ClassName)
156 %typemap(jni) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
158 %typemap(jtype) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
159 "java.util.Map<String,ClassName>"
161 MAP_COMMON(std::string, std::shared_ptr<sigrok::ClassName>, String, ClassName)
163 %typemap(out) std::map<std::string, std::shared_ptr<sigrok::ClassName> > {
164 jclass HashMap = jenv->FindClass("java/util/HashMap");
165 jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
166 jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
167 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
168 jclass Value = jenv->FindClass("org/sigrok/core/classes/" #ClassName);
169 jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V");
170 $result = jenv->NewObject(HashMap, HashMap_init);
172 for (auto entry : $1)
174 *(std::shared_ptr< sigrok::ClassName > **)&value =
175 new std::shared_ptr< sigrok::ClassName>(entry.second);
176 jenv->CallObjectMethod($result, HashMap_put,
177 jenv->NewStringUTF(entry.first.c_str()),
178 jenv->NewObject(Value, Value_init, value, true));
184 STRING_TO_SHARED_PTR_MAP(Driver)
185 STRING_TO_SHARED_PTR_MAP(InputFormat)
186 STRING_TO_SHARED_PTR_MAP(OutputFormat)
188 /* Specialisation for ConfigKey->Variant maps */
190 MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
192 %typemap(jni) std::map<const sigrok::ConfigKey, Glib::VariantBase> "jobject"
193 %typemap(jtype) std::map<const sigrok::ConfigKey, Glib::VariantBase>
194 "java.util.Map<ConfigKey,Variant>"
196 %typemap(out) std::map<const sigrok::ConfigKey *, Glib::VariantBase> {
197 jclass HashMap = jenv->FindClass("java/util/HashMap");
198 jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V");
199 jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put",
200 "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
201 jclass ConfigKey = jenv->FindClass("org/sigrok/core/classes/ConfigKey");
202 jmethodID ConfigKey_init = jenv->GetMethodID(ConfigKey, "<init>", "(JZ)V");
203 jclass Variant = jenv->FindClass("org/sigrok/core/classes/Variant");
204 jmethodID Variant_init = jenv->GetMethodID(Variant, "<init>", "(JZ)V");
205 $result = jenv->NewObject(HashMap, HashMap_init);
208 for (auto entry : $1)
210 *(const sigrok::ConfigKey **) &key = entry.first;
211 *(Glib::VariantBase **) &value = new Glib::VariantBase(entry.second);
212 jenv->CallObjectMethod($result, HashMap_put,
213 jenv->NewObject(ConfigKey, ConfigKey_init, key, false));
214 jenv->NewObject(Variant, Variant_init, value, true));
218 /* Support Driver.scan() with no arguments. */
219 %extend sigrok::Driver {
220 std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan()
222 std::map<const sigrok::ConfigKey *, Glib::VariantBase> options;
223 return $self->scan(options);
227 /* Support OutputFormat.create_output(device) with no options. */
228 %extend sigrok::OutputFormat {
229 std::shared_ptr<sigrok::Output> create_output(
230 std::shared_ptr<sigrok::Device> device)
232 std::map<std::string, Glib::VariantBase> options;
233 return $self->create_output(device, options);
237 /* Pass JNIEnv parameter to C++ extension methods requiring it. */
239 %typemap(in, numinputs=0) JNIEnv * %{
243 /* Support Java log callbacks. */
245 %typemap(javaimports) sigrok::Context
246 "import org.sigrok.core.interfaces.LogCallback;"
249 typedef jobject jlogcallback;
252 %typemap(jni) jlogcallback "jlogcallback"
253 %typemap(jtype) jlogcallback "LogCallback"
254 %typemap(jstype) jlogcallback "LogCallback"
255 %typemap(javain) jlogcallback "$javainput"
257 %extend sigrok::Context
259 void add_log_callback(JNIEnv *env, jlogcallback obj)
261 jclass obj_class = env->GetObjectClass(obj);
262 jmethodID method = env->GetMethodID(obj_class, "run",
263 "(Lorg/sigrok/core/classes/LogLevel;Ljava/lang/String;)V");
264 jclass LogLevel = (jclass) env->NewGlobalRef(
265 env->FindClass("org/sigrok/core/classes/LogLevel"));
266 jmethodID LogLevel_init = env->GetMethodID(LogLevel, "<init>", "(JZ)V");
267 jobject obj_ref = env->NewGlobalRef(obj);
269 $self->set_log_callback([=] (
270 const sigrok::LogLevel *loglevel,
274 *(const sigrok::LogLevel **) &loglevel_addr = loglevel;
275 jobject loglevel_obj = env->NewObject(
276 LogLevel, LogLevel_init, loglevel_addr, false);
277 jobject message_obj = env->NewStringUTF(message.c_str());
278 env->CallVoidMethod(obj_ref, method, loglevel_obj, message_obj);
279 if (env->ExceptionCheck())
280 throw sigrok::Error(SR_ERR);
285 /* Support Java datafeed callbacks. */
287 %typemap(javaimports) sigrok::Session
288 "import org.sigrok.core.interfaces.DatafeedCallback;"
291 typedef jobject jdatafeedcallback;
294 %typemap(jni) jdatafeedcallback "jdatafeedcallback"
295 %typemap(jtype) jdatafeedcallback "DatafeedCallback"
296 %typemap(jstype) jdatafeedcallback "DatafeedCallback"
297 %typemap(javain) jdatafeedcallback "$javainput"
299 %extend sigrok::Session
301 void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj)
303 jclass obj_class = env->GetObjectClass(obj);
304 jmethodID method = env->GetMethodID(obj_class, "run",
305 "(Lorg/sigrok/core/classes/Device;Lorg/sigrok/core/classes/Packet;)V");
306 jclass Device = (jclass) env->NewGlobalRef(
307 env->FindClass("org/sigrok/core/classes/Device"));
308 jmethodID Device_init = env->GetMethodID(Device, "<init>", "(JZ)V");
309 jclass Packet = (jclass) env->NewGlobalRef(
310 env->FindClass("org/sigrok/core/classes/Packet"));
311 jmethodID Packet_init = env->GetMethodID(Packet, "<init>", "(JZ)V");
312 jobject obj_ref = env->NewGlobalRef(obj);
314 $self->add_datafeed_callback([=] (
315 std::shared_ptr<sigrok::Device> device,
316 std::shared_ptr<sigrok::Packet> packet)
320 *(std::shared_ptr<sigrok::Device> **) &device_addr =
321 new std::shared_ptr<sigrok::Device>(device);
322 *(std::shared_ptr<sigrok::Packet> **) &packet_addr =
323 new std::shared_ptr<sigrok::Packet>(packet);
324 jobject device_obj = env->NewObject(
325 Device, Device_init, device_addr, true);
326 jobject packet_obj = env->NewObject(
327 Packet, Packet_init, packet_addr, true);
328 env->CallVoidMethod(obj_ref, method, device_obj, packet_obj);
329 if (env->ExceptionCheck())
330 throw sigrok::Error(SR_ERR);
335 /* Support Java event source callbacks. */
337 %typemap(javaimports) sigrok::EventSource
338 "import org.sigrok.core.interfaces.SourceCallback;"
341 typedef jobject jsourcecallback;
344 %typemap(jni) jsourcecallback "jsourcecallback"
345 %typemap(jtype) jsourcecallback "SourceCallback"
346 %typemap(jstype) jsourcecallback "SourceCallback"
347 %typemap(javain) jsourcecallback "$javainput"
349 %extend sigrok::EventSource
351 std::shared_ptr<sigrok::EventSource> create(
352 int fd, Glib::IOCondition events, int timeout,
353 JNIEnv *env, jsourcecallback obj)
356 jclass obj_class = env->GetObjectClass(obj);
357 jmethodID method = env->GetMethodID(obj_class, "run", "(I)V");
358 jobject obj_ref = env->NewGlobalRef(obj);
360 return sigrok::EventSource::create(fd, events, timeout, [=] (int revents)
362 bool result = env->CallBooleanMethod(obj_ref, method, revents);
363 if (env->ExceptionCheck())
364 throw sigrok::Error(SR_ERR);
370 /* Currently broken due to some std::map typemap issues. */
371 %ignore sigrok::Meta::get_config;
374 %include "bindings/swig/classes.i"