]> sigrok.org Git - libsigrok.git/blob - bindings/java/org/sigrok/core/classes/classes.i
Add a public API to list available serial ports.
[libsigrok.git] / bindings / java / org / sigrok / core / classes / classes.i
1 %module classes
2
3 /* Automatically load JNI library. */
4 %pragma(java) jniclasscode=%{
5   static {
6     System.loadLibrary("sigrok_java_core_classes");
7   }
8 %}
9
10 /* Documentation & importing interfaces. */
11 %pragma(java) jniclassimports=%{
12 /**
13  * @mainpage API Reference
14  * 
15  * Introduction
16  * ------------
17  * 
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.
20  * 
21  * Getting started
22  * ---------------
23  * 
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.
29  * 
30  * Error handling
31  * --------------
32  * 
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.
35  */
36
37 import org.sigrok.core.interfaces.LogCallback;
38 import org.sigrok.core.interfaces.DatafeedCallback;
39 import org.sigrok.core.interfaces.SourceCallback;
40 %}
41
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"
47
48 %typemap(in) int fd {
49   jclass FileDescriptor = jenv->FindClass("java/io/FileDescriptor");
50   jfieldID fd = jenv->GetFieldID(FileDescriptor, "fd", "I");
51   $1 = jenv->GetIntField($input, fd);
52 }
53
54 /* Map Glib::VariantBase to a Variant class in Java */
55 %rename(Variant) VariantBase;
56 namespace Glib {
57   class VariantBase {};
58 }
59
60 /* Map between std::vector and java.util.Vector */
61 %define VECTOR(CValue, JValue)
62
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>"
66
67 %typemap(javain,
68     pre="  $javaclassname temp$javainput = $javaclassname.convertVector($javainput);",
69     pgcppname="temp$javainput")
70   std::vector< CValue > "$javaclassname.getCPtr(temp$javainput)"
71
72 %typemap(javacode) std::vector< CValue > %{
73   static $javaclassname convertVector(java.util.Vector<JValue> in)
74   {
75     $javaclassname out = new $javaclassname();
76     for (JValue value : in)
77       out.add(value);
78     return out;
79   }
80 %}
81
82 %typemap(javaout) std::vector< CValue > {
83   return (java.util.Vector<JValue>)$jnicall;
84 }
85
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);
94   jlong value;
95   for (auto entry : $1)
96   {
97     *(CValue **) &value = new CValue(entry);
98     jenv->CallObjectMethod($result, Vector_add,
99       jenv->NewObject(Value, Value_init, value, true));
100   }
101 }
102
103 %enddef
104
105 VECTOR(std::shared_ptr<sigrok::Channel>, Channel)
106 VECTOR(std::shared_ptr<sigrok::HardwareDevice>, HardwareDevice)
107
108 /* Common macro for mapping between std::map and java.util.Map */
109
110 %define MAP_COMMON(CKey, CValue, JKey, JValue)
111
112 %typemap(jstype) std::map< CKey, CValue >
113   "java.util.Map<JKey, JValue>"
114
115 %typemap(javain,
116     pre="  $javaclassname temp$javainput = $javaclassname.convertMap($javainput);",
117     pgcppname="temp$javainput")
118   std::map< CKey, CValue > "$javaclassname.getCPtr(temp$javainput)"
119
120 %typemap(javacode) std::map< CKey, CValue > %{
121   static $javaclassname convertMap(java.util.Map<JKey,JValue> in)
122   {
123     $javaclassname out = new $javaclassname();
124     for (java.util.Map.Entry<JKey, JValue> entry : in.entrySet())
125       out.set(entry.getKey(), entry.getValue());
126     return out;
127   }
128 %}
129
130 %typemap(javaout) std::map< CKey, CValue > {
131   return (java.util.Map<JKey, JValue>)$jnicall;
132 }
133
134 %enddef
135
136 /* Specialisation for string->string maps. */
137
138 MAP_COMMON(std::string, std::string, String, String)
139
140 %typemap(jni) std::map<std::string, std::string>
141   "jobject"
142 %typemap(jtype) std::map<std::string, std::string>
143   "java.util.Map<String,String>"
144
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()));
155 }
156
157 /* Specialisation macro for string->shared_ptr maps. */
158
159 %define STRING_TO_SHARED_PTR_MAP(ClassName)
160
161 %typemap(jni) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
162   "jobject"
163 %typemap(jtype) std::map<std::string, std::shared_ptr<sigrok::ClassName> >
164   "java.util.Map<String,ClassName>"
165
166 MAP_COMMON(std::string, std::shared_ptr<sigrok::ClassName>, String, ClassName)
167
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);
176   jlong value;
177   for (auto entry : $1)
178   {
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));
184   }
185 }
186
187 %enddef
188
189 STRING_TO_SHARED_PTR_MAP(Driver)
190 STRING_TO_SHARED_PTR_MAP(InputFormat)
191 STRING_TO_SHARED_PTR_MAP(OutputFormat)
192
193 /* Specialisation for ConfigKey->Variant maps */
194
195 MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant)
196
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>"
200
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);
211   jlong key;
212   jlong value;
213   for (auto entry : $1)
214   {
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));
220   }
221 }
222
223 /* Support Driver.scan() with no arguments. */
224 %extend sigrok::Driver {
225   std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan()
226   {
227     std::map<const sigrok::ConfigKey *, Glib::VariantBase> options;
228     return $self->scan(options);
229   }
230 }
231
232 /* Support InputFormat.create_input() with no options. */
233 %extend sigrok::InputFormat {
234   std::shared_ptr<sigrok::Input> create_input()
235   {
236     std::map<std::string, Glib::VariantBase> options;
237     return $self->create_input(options);
238   }
239 }
240
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)
245   {
246     std::map<std::string, Glib::VariantBase> options;
247     return $self->create_output(device, options);
248   }
249 }
250
251 /* Pass JNIEnv parameter to C++ extension methods requiring it. */
252
253 %typemap(in, numinputs=0) JNIEnv * %{
254    $1 = jenv;
255 %} 
256
257 /* Support Java log callbacks. */
258
259 %typemap(javaimports) sigrok::Context
260   "import org.sigrok.core.interfaces.LogCallback;"
261
262 %inline {
263 typedef jobject jlogcallback;
264 }
265
266 %typemap(jni) jlogcallback "jlogcallback"
267 %typemap(jtype) jlogcallback "LogCallback"
268 %typemap(jstype) jlogcallback "LogCallback"
269 %typemap(javain) jlogcallback "$javainput"
270
271 %extend sigrok::Context
272 {
273   void add_log_callback(JNIEnv *env, jlogcallback obj)
274   {
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);
282
283     $self->set_log_callback([=] (
284       const sigrok::LogLevel *loglevel,
285       std::string message)
286     {
287       jlong loglevel_addr;
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);
295     });
296   }
297 }
298
299 /* Support Java datafeed callbacks. */
300
301 %typemap(javaimports) sigrok::Session
302   "import org.sigrok.core.interfaces.DatafeedCallback;"
303
304 %inline {
305 typedef jobject jdatafeedcallback;
306 }
307
308 %typemap(jni) jdatafeedcallback "jdatafeedcallback"
309 %typemap(jtype) jdatafeedcallback "DatafeedCallback"
310 %typemap(jstype) jdatafeedcallback "DatafeedCallback"
311 %typemap(javain) jdatafeedcallback "$javainput"
312
313 %extend sigrok::Session
314 {
315   void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj)
316   {
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);
327
328     $self->add_datafeed_callback([=] (
329       std::shared_ptr<sigrok::Device> device,
330       std::shared_ptr<sigrok::Packet> packet)
331     {
332       jlong device_addr;
333       jlong packet_addr;
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);
345     });
346   }
347 }
348
349 /* Support Java event source callbacks. */
350
351 %typemap(javaimports) sigrok::EventSource
352   "import org.sigrok.core.interfaces.SourceCallback;"
353
354 %inline {
355 typedef jobject jsourcecallback;
356 }
357
358 %typemap(jni) jsourcecallback "jsourcecallback"
359 %typemap(jtype) jsourcecallback "SourceCallback"
360 %typemap(jstype) jsourcecallback "SourceCallback"
361 %typemap(javain) jsourcecallback "$javainput"
362
363 %extend sigrok::EventSource
364 {
365   std::shared_ptr<sigrok::EventSource> create(
366     int fd, Glib::IOCondition events, int timeout,
367     JNIEnv *env, jsourcecallback obj)
368   {
369     (void) $self;
370     jclass obj_class = env->GetObjectClass(obj);
371     jmethodID method = env->GetMethodID(obj_class, "run", "(I)V");
372     jobject obj_ref = env->NewGlobalRef(obj);
373
374     return sigrok::EventSource::create(fd, events, timeout, [=] (int revents)
375     {
376       bool result = env->CallBooleanMethod(obj_ref, method, revents);
377       if (env->ExceptionCheck())
378         throw sigrok::Error(SR_ERR);
379       return result;
380     });
381   }
382 }
383
384 %include "doc.i"
385
386 %define %attributevector(Class, Type, Name, Get)
387 %attributeval(sigrok::Class, Type, Name, Get);
388 %enddef
389
390 %define %attributemap(Class, Type, Name, Get)
391 %attributeval(sigrok::Class, Type, Name, Get);
392 %enddef
393
394 %define %enumextras(Class)
395 %enddef
396
397 /* Ignore this for now, needs a fix. */
398 %ignore sigrok::Context::create_analog_packet;
399
400 %include "bindings/swig/classes.i"
401