]>
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 | |
52ff4f6a | 19 | * functionality in libsigrok. It is built on top of the libsigrokcxx C++ API. |
8a314e90 ML |
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; | |
90bd7656 ML |
39 | %} |
40 | ||
9fcf4d0b ML |
41 | /* Map Glib::VariantBase to a Variant class in Java */ |
42 | %rename(Variant) VariantBase; | |
43 | namespace Glib { | |
44 | class VariantBase {}; | |
45 | } | |
46 | ||
c7855def ML |
47 | %include "bindings/swig/templates.i" |
48 | ||
9fcf4d0b ML |
49 | /* Map between std::vector and java.util.Vector */ |
50 | %define VECTOR(CValue, JValue) | |
51 | ||
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>" | |
55 | ||
56 | %typemap(javain, | |
f3095e7e ML |
57 | pre=" $javaclassname temp$javainput = new $javaclassname(); |
58 | for (JValue value : $javainput) | |
59 | temp$javainput.add(value);", | |
9fcf4d0b ML |
60 | pgcppname="temp$javainput") |
61 | std::vector< CValue > "$javaclassname.getCPtr(temp$javainput)" | |
62 | ||
9fcf4d0b ML |
63 | %typemap(javaout) std::vector< CValue > { |
64 | return (java.util.Vector<JValue>)$jnicall; | |
65 | } | |
66 | ||
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); | |
75 | jlong value; | |
76 | for (auto entry : $1) | |
77 | { | |
78 | *(CValue **) &value = new CValue(entry); | |
79 | jenv->CallObjectMethod($result, Vector_add, | |
80 | jenv->NewObject(Value, Value_init, value, true)); | |
81 | } | |
82 | } | |
83 | ||
84 | %enddef | |
85 | ||
86 | VECTOR(std::shared_ptr<sigrok::Channel>, Channel) | |
87 | VECTOR(std::shared_ptr<sigrok::HardwareDevice>, HardwareDevice) | |
88 | ||
89 | /* Common macro for mapping between std::map and java.util.Map */ | |
90 | ||
91 | %define MAP_COMMON(CKey, CValue, JKey, JValue) | |
92 | ||
93 | %typemap(jstype) std::map< CKey, CValue > | |
94 | "java.util.Map<JKey, JValue>" | |
95 | ||
96 | %typemap(javain, | |
f3095e7e ML |
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());", | |
9fcf4d0b ML |
100 | pgcppname="temp$javainput") |
101 | std::map< CKey, CValue > "$javaclassname.getCPtr(temp$javainput)" | |
102 | ||
9fcf4d0b ML |
103 | %typemap(javaout) std::map< CKey, CValue > { |
104 | return (java.util.Map<JKey, JValue>)$jnicall; | |
105 | } | |
106 | ||
107 | %enddef | |
108 | ||
109 | /* Specialisation for string->string maps. */ | |
110 | ||
111 | MAP_COMMON(std::string, std::string, String, String) | |
112 | ||
24287ea9 AJ |
113 | %typemap(jni) std::map<std::string, std::string> |
114 | "jobject" | |
115 | %typemap(jtype) std::map<std::string, std::string> | |
116 | "java.util.Map<String,String>" | |
117 | ||
9fcf4d0b ML |
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())); | |
128 | } | |
129 | ||
130 | /* Specialisation macro for string->shared_ptr maps. */ | |
131 | ||
132 | %define STRING_TO_SHARED_PTR_MAP(ClassName) | |
133 | ||
134 | %typemap(jni) std::map<std::string, std::shared_ptr<sigrok::ClassName> > | |
135 | "jobject" | |
136 | %typemap(jtype) std::map<std::string, std::shared_ptr<sigrok::ClassName> > | |
137 | "java.util.Map<String,ClassName>" | |
138 | ||
139 | MAP_COMMON(std::string, std::shared_ptr<sigrok::ClassName>, String, ClassName) | |
140 | ||
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); | |
149 | jlong value; | |
150 | for (auto entry : $1) | |
151 | { | |
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)); | |
157 | } | |
158 | } | |
159 | ||
160 | %enddef | |
161 | ||
162 | STRING_TO_SHARED_PTR_MAP(Driver) | |
163 | STRING_TO_SHARED_PTR_MAP(InputFormat) | |
164 | STRING_TO_SHARED_PTR_MAP(OutputFormat) | |
165 | ||
166 | /* Specialisation for ConfigKey->Variant maps */ | |
167 | ||
168 | MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant) | |
169 | ||
2928f47d ML |
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>" | |
173 | ||
9fcf4d0b ML |
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); | |
184 | jlong key; | |
185 | jlong value; | |
186 | for (auto entry : $1) | |
187 | { | |
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)); | |
193 | } | |
194 | } | |
195 | ||
196 | /* Support Driver.scan() with no arguments. */ | |
85aa1b59 ML |
197 | %ignore sigrok::Driver::scan; |
198 | ||
9fcf4d0b ML |
199 | %extend sigrok::Driver { |
200 | std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan() | |
201 | { | |
202 | std::map<const sigrok::ConfigKey *, Glib::VariantBase> options; | |
203 | return $self->scan(options); | |
204 | } | |
85aa1b59 ML |
205 | |
206 | std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan( | |
207 | std::map<const sigrok::ConfigKey *, Glib::VariantBase> options) | |
208 | { | |
209 | return $self->scan(options); | |
210 | } | |
9fcf4d0b ML |
211 | } |
212 | ||
85aa1b59 ML |
213 | /* Support InputFormat.create_input() with or without options. */ |
214 | %ignore sigrok::InputFormat::create_input; | |
215 | ||
ca3291e3 ML |
216 | %extend sigrok::InputFormat { |
217 | std::shared_ptr<sigrok::Input> create_input() | |
218 | { | |
85aa1b59 ML |
219 | return $self->create_input(); |
220 | } | |
221 | ||
222 | std::shared_ptr<sigrok::Input> create_input( | |
223 | std::map<std::string, Glib::VariantBase> options) | |
224 | { | |
ca3291e3 ML |
225 | return $self->create_input(options); |
226 | } | |
227 | } | |
228 | ||
85aa1b59 ML |
229 | /* Support OutputFormat.create_output() with or without options. */ |
230 | %ignore sigrok::OutputFormat::create_output; | |
231 | ||
9fcf4d0b ML |
232 | %extend sigrok::OutputFormat { |
233 | std::shared_ptr<sigrok::Output> create_output( | |
234 | std::shared_ptr<sigrok::Device> device) | |
235 | { | |
85aa1b59 ML |
236 | return $self->create_output(device); |
237 | } | |
238 | ||
239 | std::shared_ptr<sigrok::Output> create_output( | |
240 | std::shared_ptr<sigrok::Device> device, | |
241 | std::map<std::string, Glib::VariantBase> options) | |
242 | { | |
9fcf4d0b ML |
243 | return $self->create_output(device, options); |
244 | } | |
85aa1b59 ML |
245 | |
246 | std::shared_ptr<sigrok::Output> create_output( | |
247 | std::string filename, | |
248 | std::shared_ptr<sigrok::Device> device) | |
249 | { | |
250 | return $self->create_output(filename, device); | |
251 | } | |
252 | ||
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) | |
257 | { | |
258 | return $self->create_output(filename, device, options); | |
259 | } | |
9fcf4d0b ML |
260 | } |
261 | ||
262 | /* Pass JNIEnv parameter to C++ extension methods requiring it. */ | |
263 | ||
264 | %typemap(in, numinputs=0) JNIEnv * %{ | |
265 | $1 = jenv; | |
266 | %} | |
267 | ||
268 | /* Support Java log callbacks. */ | |
269 | ||
90bd7656 ML |
270 | %typemap(javaimports) sigrok::Context |
271 | "import org.sigrok.core.interfaces.LogCallback;" | |
272 | ||
9fcf4d0b ML |
273 | %inline { |
274 | typedef jobject jlogcallback; | |
275 | } | |
276 | ||
277 | %typemap(jni) jlogcallback "jlogcallback" | |
278 | %typemap(jtype) jlogcallback "LogCallback" | |
279 | %typemap(jstype) jlogcallback "LogCallback" | |
280 | %typemap(javain) jlogcallback "$javainput" | |
281 | ||
282 | %extend sigrok::Context | |
283 | { | |
284 | void add_log_callback(JNIEnv *env, jlogcallback obj) | |
285 | { | |
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); | |
293 | ||
294 | $self->set_log_callback([=] ( | |
295 | const sigrok::LogLevel *loglevel, | |
296 | std::string message) | |
297 | { | |
298 | jlong loglevel_addr; | |
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); | |
306 | }); | |
307 | } | |
308 | } | |
309 | ||
310 | /* Support Java datafeed callbacks. */ | |
311 | ||
90bd7656 ML |
312 | %typemap(javaimports) sigrok::Session |
313 | "import org.sigrok.core.interfaces.DatafeedCallback;" | |
314 | ||
9fcf4d0b ML |
315 | %inline { |
316 | typedef jobject jdatafeedcallback; | |
317 | } | |
318 | ||
319 | %typemap(jni) jdatafeedcallback "jdatafeedcallback" | |
320 | %typemap(jtype) jdatafeedcallback "DatafeedCallback" | |
321 | %typemap(jstype) jdatafeedcallback "DatafeedCallback" | |
322 | %typemap(javain) jdatafeedcallback "$javainput" | |
323 | ||
324 | %extend sigrok::Session | |
325 | { | |
326 | void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj) | |
327 | { | |
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); | |
338 | ||
339 | $self->add_datafeed_callback([=] ( | |
340 | std::shared_ptr<sigrok::Device> device, | |
341 | std::shared_ptr<sigrok::Packet> packet) | |
342 | { | |
343 | jlong device_addr; | |
344 | jlong packet_addr; | |
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); | |
356 | }); | |
357 | } | |
358 | } | |
359 | ||
bd4fda24 | 360 | %include "doc.i" |
062430a2 ML |
361 | |
362 | %define %attributevector(Class, Type, Name, Get) | |
363 | %attributeval(sigrok::Class, Type, Name, Get); | |
364 | %enddef | |
365 | ||
366 | %define %attributemap(Class, Type, Name, Get) | |
367 | %attributeval(sigrok::Class, Type, Name, Get); | |
368 | %enddef | |
369 | ||
7a36ceac ML |
370 | %define %enumextras(Class) |
371 | %enddef | |
372 | ||
c7855def | 373 | /* Ignore these for now, need fixes. */ |
dd13d47a | 374 | %ignore sigrok::Context::create_analog_packet; |
c7855def | 375 | %ignore sigrok::Context::create_meta_packet; |
b71356d6 | 376 | |
9fcf4d0b | 377 | %include "bindings/swig/classes.i" |
b71356d6 | 378 |