]>
Commit | Line | Data |
---|---|---|
9fcf4d0b ML |
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 | ||
8a314e90 | 10 | /* Documentation & importing interfaces. */ |
90bd7656 | 11 | %pragma(java) jniclassimports=%{ |
8a314e90 ML |
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; | |
90bd7656 ML |
40 | %} |
41 | ||
9fcf4d0b ML |
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(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())); | |
150 | } | |
151 | ||
152 | /* Specialisation macro for string->shared_ptr maps. */ | |
153 | ||
154 | %define STRING_TO_SHARED_PTR_MAP(ClassName) | |
155 | ||
156 | %typemap(jni) std::map<std::string, std::shared_ptr<sigrok::ClassName> > | |
157 | "jobject" | |
158 | %typemap(jtype) std::map<std::string, std::shared_ptr<sigrok::ClassName> > | |
159 | "java.util.Map<String,ClassName>" | |
160 | ||
161 | MAP_COMMON(std::string, std::shared_ptr<sigrok::ClassName>, String, ClassName) | |
162 | ||
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); | |
171 | jlong value; | |
172 | for (auto entry : $1) | |
173 | { | |
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)); | |
179 | } | |
180 | } | |
181 | ||
182 | %enddef | |
183 | ||
184 | STRING_TO_SHARED_PTR_MAP(Driver) | |
185 | STRING_TO_SHARED_PTR_MAP(InputFormat) | |
186 | STRING_TO_SHARED_PTR_MAP(OutputFormat) | |
187 | ||
188 | /* Specialisation for ConfigKey->Variant maps */ | |
189 | ||
190 | MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant) | |
191 | ||
2928f47d ML |
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>" | |
195 | ||
9fcf4d0b ML |
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); | |
206 | jlong key; | |
207 | jlong value; | |
208 | for (auto entry : $1) | |
209 | { | |
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)); | |
215 | } | |
216 | } | |
217 | ||
218 | /* Support Driver.scan() with no arguments. */ | |
219 | %extend sigrok::Driver { | |
220 | std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan() | |
221 | { | |
222 | std::map<const sigrok::ConfigKey *, Glib::VariantBase> options; | |
223 | return $self->scan(options); | |
224 | } | |
225 | } | |
226 | ||
ca3291e3 ML |
227 | /* Support InputFormat.create_input() with no options. */ |
228 | %extend sigrok::InputFormat { | |
229 | std::shared_ptr<sigrok::Input> create_input() | |
230 | { | |
231 | std::map<std::string, Glib::VariantBase> options; | |
232 | return $self->create_input(options); | |
233 | } | |
234 | } | |
235 | ||
9fcf4d0b ML |
236 | /* Support OutputFormat.create_output(device) with no options. */ |
237 | %extend sigrok::OutputFormat { | |
238 | std::shared_ptr<sigrok::Output> create_output( | |
239 | std::shared_ptr<sigrok::Device> device) | |
240 | { | |
7a958e2a | 241 | std::map<std::string, Glib::VariantBase> options; |
9fcf4d0b ML |
242 | return $self->create_output(device, options); |
243 | } | |
244 | } | |
245 | ||
246 | /* Pass JNIEnv parameter to C++ extension methods requiring it. */ | |
247 | ||
248 | %typemap(in, numinputs=0) JNIEnv * %{ | |
249 | $1 = jenv; | |
250 | %} | |
251 | ||
252 | /* Support Java log callbacks. */ | |
253 | ||
90bd7656 ML |
254 | %typemap(javaimports) sigrok::Context |
255 | "import org.sigrok.core.interfaces.LogCallback;" | |
256 | ||
9fcf4d0b ML |
257 | %inline { |
258 | typedef jobject jlogcallback; | |
259 | } | |
260 | ||
261 | %typemap(jni) jlogcallback "jlogcallback" | |
262 | %typemap(jtype) jlogcallback "LogCallback" | |
263 | %typemap(jstype) jlogcallback "LogCallback" | |
264 | %typemap(javain) jlogcallback "$javainput" | |
265 | ||
266 | %extend sigrok::Context | |
267 | { | |
268 | void add_log_callback(JNIEnv *env, jlogcallback obj) | |
269 | { | |
270 | jclass obj_class = env->GetObjectClass(obj); | |
271 | jmethodID method = env->GetMethodID(obj_class, "run", | |
272 | "(Lorg/sigrok/core/classes/LogLevel;Ljava/lang/String;)V"); | |
273 | jclass LogLevel = (jclass) env->NewGlobalRef( | |
274 | env->FindClass("org/sigrok/core/classes/LogLevel")); | |
275 | jmethodID LogLevel_init = env->GetMethodID(LogLevel, "<init>", "(JZ)V"); | |
276 | jobject obj_ref = env->NewGlobalRef(obj); | |
277 | ||
278 | $self->set_log_callback([=] ( | |
279 | const sigrok::LogLevel *loglevel, | |
280 | std::string message) | |
281 | { | |
282 | jlong loglevel_addr; | |
283 | *(const sigrok::LogLevel **) &loglevel_addr = loglevel; | |
284 | jobject loglevel_obj = env->NewObject( | |
285 | LogLevel, LogLevel_init, loglevel_addr, false); | |
286 | jobject message_obj = env->NewStringUTF(message.c_str()); | |
287 | env->CallVoidMethod(obj_ref, method, loglevel_obj, message_obj); | |
288 | if (env->ExceptionCheck()) | |
289 | throw sigrok::Error(SR_ERR); | |
290 | }); | |
291 | } | |
292 | } | |
293 | ||
294 | /* Support Java datafeed callbacks. */ | |
295 | ||
90bd7656 ML |
296 | %typemap(javaimports) sigrok::Session |
297 | "import org.sigrok.core.interfaces.DatafeedCallback;" | |
298 | ||
9fcf4d0b ML |
299 | %inline { |
300 | typedef jobject jdatafeedcallback; | |
301 | } | |
302 | ||
303 | %typemap(jni) jdatafeedcallback "jdatafeedcallback" | |
304 | %typemap(jtype) jdatafeedcallback "DatafeedCallback" | |
305 | %typemap(jstype) jdatafeedcallback "DatafeedCallback" | |
306 | %typemap(javain) jdatafeedcallback "$javainput" | |
307 | ||
308 | %extend sigrok::Session | |
309 | { | |
310 | void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj) | |
311 | { | |
312 | jclass obj_class = env->GetObjectClass(obj); | |
313 | jmethodID method = env->GetMethodID(obj_class, "run", | |
314 | "(Lorg/sigrok/core/classes/Device;Lorg/sigrok/core/classes/Packet;)V"); | |
315 | jclass Device = (jclass) env->NewGlobalRef( | |
316 | env->FindClass("org/sigrok/core/classes/Device")); | |
317 | jmethodID Device_init = env->GetMethodID(Device, "<init>", "(JZ)V"); | |
318 | jclass Packet = (jclass) env->NewGlobalRef( | |
319 | env->FindClass("org/sigrok/core/classes/Packet")); | |
320 | jmethodID Packet_init = env->GetMethodID(Packet, "<init>", "(JZ)V"); | |
321 | jobject obj_ref = env->NewGlobalRef(obj); | |
322 | ||
323 | $self->add_datafeed_callback([=] ( | |
324 | std::shared_ptr<sigrok::Device> device, | |
325 | std::shared_ptr<sigrok::Packet> packet) | |
326 | { | |
327 | jlong device_addr; | |
328 | jlong packet_addr; | |
329 | *(std::shared_ptr<sigrok::Device> **) &device_addr = | |
330 | new std::shared_ptr<sigrok::Device>(device); | |
331 | *(std::shared_ptr<sigrok::Packet> **) &packet_addr = | |
332 | new std::shared_ptr<sigrok::Packet>(packet); | |
333 | jobject device_obj = env->NewObject( | |
334 | Device, Device_init, device_addr, true); | |
335 | jobject packet_obj = env->NewObject( | |
336 | Packet, Packet_init, packet_addr, true); | |
337 | env->CallVoidMethod(obj_ref, method, device_obj, packet_obj); | |
338 | if (env->ExceptionCheck()) | |
339 | throw sigrok::Error(SR_ERR); | |
340 | }); | |
341 | } | |
342 | } | |
343 | ||
344 | /* Support Java event source callbacks. */ | |
345 | ||
90bd7656 ML |
346 | %typemap(javaimports) sigrok::EventSource |
347 | "import org.sigrok.core.interfaces.SourceCallback;" | |
348 | ||
9fcf4d0b ML |
349 | %inline { |
350 | typedef jobject jsourcecallback; | |
351 | } | |
352 | ||
353 | %typemap(jni) jsourcecallback "jsourcecallback" | |
354 | %typemap(jtype) jsourcecallback "SourceCallback" | |
355 | %typemap(jstype) jsourcecallback "SourceCallback" | |
356 | %typemap(javain) jsourcecallback "$javainput" | |
357 | ||
358 | %extend sigrok::EventSource | |
359 | { | |
360 | std::shared_ptr<sigrok::EventSource> create( | |
361 | int fd, Glib::IOCondition events, int timeout, | |
362 | JNIEnv *env, jsourcecallback obj) | |
363 | { | |
364 | (void) $self; | |
365 | jclass obj_class = env->GetObjectClass(obj); | |
366 | jmethodID method = env->GetMethodID(obj_class, "run", "(I)V"); | |
367 | jobject obj_ref = env->NewGlobalRef(obj); | |
368 | ||
369 | return sigrok::EventSource::create(fd, events, timeout, [=] (int revents) | |
370 | { | |
371 | bool result = env->CallBooleanMethod(obj_ref, method, revents); | |
372 | if (env->ExceptionCheck()) | |
373 | throw sigrok::Error(SR_ERR); | |
374 | return result; | |
375 | }); | |
376 | } | |
377 | } | |
378 | ||
bd4fda24 | 379 | %include "doc.i" |
062430a2 ML |
380 | |
381 | %define %attributevector(Class, Type, Name, Get) | |
382 | %attributeval(sigrok::Class, Type, Name, Get); | |
383 | %enddef | |
384 | ||
385 | %define %attributemap(Class, Type, Name, Get) | |
386 | %attributeval(sigrok::Class, Type, Name, Get); | |
387 | %enddef | |
388 | ||
9fcf4d0b | 389 | %include "bindings/swig/classes.i" |