]>
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 | ||
47 | /* Map between std::vector and java.util.Vector */ | |
48 | %define VECTOR(CValue, JValue) | |
49 | ||
50 | %typemap(jni) std::vector< CValue > "jobject" | |
51 | %typemap(jtype) std::vector< CValue > "java.util.Vector<JValue>" | |
52 | %typemap(jstype) std::vector< CValue > "java.util.Vector<JValue>" | |
53 | ||
54 | %typemap(javain, | |
55 | pre=" $javaclassname temp$javainput = $javaclassname.convertVector($javainput);", | |
56 | pgcppname="temp$javainput") | |
57 | std::vector< CValue > "$javaclassname.getCPtr(temp$javainput)" | |
58 | ||
59 | %typemap(javacode) std::vector< CValue > %{ | |
60 | static $javaclassname convertVector(java.util.Vector<JValue> in) | |
61 | { | |
62 | $javaclassname out = new $javaclassname(); | |
63 | for (JValue value : in) | |
64 | out.add(value); | |
65 | return out; | |
66 | } | |
67 | %} | |
68 | ||
69 | %typemap(javaout) std::vector< CValue > { | |
70 | return (java.util.Vector<JValue>)$jnicall; | |
71 | } | |
72 | ||
73 | %typemap(out) std::vector< CValue > { | |
74 | jclass Vector = jenv->FindClass("java/util/Vector"); | |
75 | jmethodID Vector_init = jenv->GetMethodID(Vector, "<init>", "()V"); | |
76 | jmethodID Vector_add = jenv->GetMethodID(Vector, "add", | |
77 | "(Ljava/lang/Object;)Z"); | |
78 | jclass Value = jenv->FindClass("org/sigrok/core/classes/" #JValue); | |
79 | jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V"); | |
80 | $result = jenv->NewObject(Vector, Vector_init); | |
81 | jlong value; | |
82 | for (auto entry : $1) | |
83 | { | |
84 | *(CValue **) &value = new CValue(entry); | |
85 | jenv->CallObjectMethod($result, Vector_add, | |
86 | jenv->NewObject(Value, Value_init, value, true)); | |
87 | } | |
88 | } | |
89 | ||
90 | %enddef | |
91 | ||
92 | VECTOR(std::shared_ptr<sigrok::Channel>, Channel) | |
93 | VECTOR(std::shared_ptr<sigrok::HardwareDevice>, HardwareDevice) | |
94 | ||
95 | /* Common macro for mapping between std::map and java.util.Map */ | |
96 | ||
97 | %define MAP_COMMON(CKey, CValue, JKey, JValue) | |
98 | ||
99 | %typemap(jstype) std::map< CKey, CValue > | |
100 | "java.util.Map<JKey, JValue>" | |
101 | ||
102 | %typemap(javain, | |
103 | pre=" $javaclassname temp$javainput = $javaclassname.convertMap($javainput);", | |
104 | pgcppname="temp$javainput") | |
105 | std::map< CKey, CValue > "$javaclassname.getCPtr(temp$javainput)" | |
106 | ||
107 | %typemap(javacode) std::map< CKey, CValue > %{ | |
108 | static $javaclassname convertMap(java.util.Map<JKey,JValue> in) | |
109 | { | |
110 | $javaclassname out = new $javaclassname(); | |
111 | for (java.util.Map.Entry<JKey, JValue> entry : in.entrySet()) | |
112 | out.set(entry.getKey(), entry.getValue()); | |
113 | return out; | |
114 | } | |
115 | %} | |
116 | ||
117 | %typemap(javaout) std::map< CKey, CValue > { | |
118 | return (java.util.Map<JKey, JValue>)$jnicall; | |
119 | } | |
120 | ||
121 | %enddef | |
122 | ||
123 | /* Specialisation for string->string maps. */ | |
124 | ||
125 | MAP_COMMON(std::string, std::string, String, String) | |
126 | ||
24287ea9 AJ |
127 | %typemap(jni) std::map<std::string, std::string> |
128 | "jobject" | |
129 | %typemap(jtype) std::map<std::string, std::string> | |
130 | "java.util.Map<String,String>" | |
131 | ||
9fcf4d0b ML |
132 | %typemap(out) std::map<std::string, std::string> { |
133 | jclass HashMap = jenv->FindClass("java/util/HashMap"); | |
134 | jmethodID init = jenv->GetMethodID(HashMap, "<init>", "()V"); | |
135 | jmethodID put = jenv->GetMethodID(HashMap, "put", | |
136 | "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); | |
137 | $result = jenv->NewObject(HashMap, init); | |
138 | for (auto entry : $1) | |
139 | jenv->CallObjectMethod($result, put, | |
140 | jenv->NewStringUTF(entry.first.c_str()), | |
141 | jenv->NewStringUTF(entry.second.c_str())); | |
142 | } | |
143 | ||
144 | /* Specialisation macro for string->shared_ptr maps. */ | |
145 | ||
146 | %define STRING_TO_SHARED_PTR_MAP(ClassName) | |
147 | ||
148 | %typemap(jni) std::map<std::string, std::shared_ptr<sigrok::ClassName> > | |
149 | "jobject" | |
150 | %typemap(jtype) std::map<std::string, std::shared_ptr<sigrok::ClassName> > | |
151 | "java.util.Map<String,ClassName>" | |
152 | ||
153 | MAP_COMMON(std::string, std::shared_ptr<sigrok::ClassName>, String, ClassName) | |
154 | ||
155 | %typemap(out) std::map<std::string, std::shared_ptr<sigrok::ClassName> > { | |
156 | jclass HashMap = jenv->FindClass("java/util/HashMap"); | |
157 | jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V"); | |
158 | jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put", | |
159 | "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); | |
160 | jclass Value = jenv->FindClass("org/sigrok/core/classes/" #ClassName); | |
161 | jmethodID Value_init = jenv->GetMethodID(Value, "<init>", "(JZ)V"); | |
162 | $result = jenv->NewObject(HashMap, HashMap_init); | |
163 | jlong value; | |
164 | for (auto entry : $1) | |
165 | { | |
166 | *(std::shared_ptr< sigrok::ClassName > **)&value = | |
167 | new std::shared_ptr< sigrok::ClassName>(entry.second); | |
168 | jenv->CallObjectMethod($result, HashMap_put, | |
169 | jenv->NewStringUTF(entry.first.c_str()), | |
170 | jenv->NewObject(Value, Value_init, value, true)); | |
171 | } | |
172 | } | |
173 | ||
174 | %enddef | |
175 | ||
176 | STRING_TO_SHARED_PTR_MAP(Driver) | |
177 | STRING_TO_SHARED_PTR_MAP(InputFormat) | |
178 | STRING_TO_SHARED_PTR_MAP(OutputFormat) | |
179 | ||
180 | /* Specialisation for ConfigKey->Variant maps */ | |
181 | ||
182 | MAP_COMMON(const sigrok::ConfigKey *, Glib::VariantBase, ConfigKey, Variant) | |
183 | ||
2928f47d ML |
184 | %typemap(jni) std::map<const sigrok::ConfigKey, Glib::VariantBase> "jobject" |
185 | %typemap(jtype) std::map<const sigrok::ConfigKey, Glib::VariantBase> | |
186 | "java.util.Map<ConfigKey,Variant>" | |
187 | ||
9fcf4d0b ML |
188 | %typemap(out) std::map<const sigrok::ConfigKey *, Glib::VariantBase> { |
189 | jclass HashMap = jenv->FindClass("java/util/HashMap"); | |
190 | jmethodID HashMap_init = jenv->GetMethodID(HashMap, "<init>", "()V"); | |
191 | jmethodID HashMap_put = jenv->GetMethodID(HashMap, "put", | |
192 | "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); | |
193 | jclass ConfigKey = jenv->FindClass("org/sigrok/core/classes/ConfigKey"); | |
194 | jmethodID ConfigKey_init = jenv->GetMethodID(ConfigKey, "<init>", "(JZ)V"); | |
195 | jclass Variant = jenv->FindClass("org/sigrok/core/classes/Variant"); | |
196 | jmethodID Variant_init = jenv->GetMethodID(Variant, "<init>", "(JZ)V"); | |
197 | $result = jenv->NewObject(HashMap, HashMap_init); | |
198 | jlong key; | |
199 | jlong value; | |
200 | for (auto entry : $1) | |
201 | { | |
202 | *(const sigrok::ConfigKey **) &key = entry.first; | |
203 | *(Glib::VariantBase **) &value = new Glib::VariantBase(entry.second); | |
204 | jenv->CallObjectMethod($result, HashMap_put, | |
205 | jenv->NewObject(ConfigKey, ConfigKey_init, key, false)); | |
206 | jenv->NewObject(Variant, Variant_init, value, true)); | |
207 | } | |
208 | } | |
209 | ||
210 | /* Support Driver.scan() with no arguments. */ | |
211 | %extend sigrok::Driver { | |
212 | std::vector<std::shared_ptr<sigrok::HardwareDevice> > scan() | |
213 | { | |
214 | std::map<const sigrok::ConfigKey *, Glib::VariantBase> options; | |
215 | return $self->scan(options); | |
216 | } | |
217 | } | |
218 | ||
ca3291e3 ML |
219 | /* Support InputFormat.create_input() with no options. */ |
220 | %extend sigrok::InputFormat { | |
221 | std::shared_ptr<sigrok::Input> create_input() | |
222 | { | |
223 | std::map<std::string, Glib::VariantBase> options; | |
224 | return $self->create_input(options); | |
225 | } | |
226 | } | |
227 | ||
9fcf4d0b ML |
228 | /* Support OutputFormat.create_output(device) with no options. */ |
229 | %extend sigrok::OutputFormat { | |
230 | std::shared_ptr<sigrok::Output> create_output( | |
231 | std::shared_ptr<sigrok::Device> device) | |
232 | { | |
7a958e2a | 233 | std::map<std::string, Glib::VariantBase> options; |
9fcf4d0b ML |
234 | return $self->create_output(device, options); |
235 | } | |
236 | } | |
237 | ||
238 | /* Pass JNIEnv parameter to C++ extension methods requiring it. */ | |
239 | ||
240 | %typemap(in, numinputs=0) JNIEnv * %{ | |
241 | $1 = jenv; | |
242 | %} | |
243 | ||
244 | /* Support Java log callbacks. */ | |
245 | ||
90bd7656 ML |
246 | %typemap(javaimports) sigrok::Context |
247 | "import org.sigrok.core.interfaces.LogCallback;" | |
248 | ||
9fcf4d0b ML |
249 | %inline { |
250 | typedef jobject jlogcallback; | |
251 | } | |
252 | ||
253 | %typemap(jni) jlogcallback "jlogcallback" | |
254 | %typemap(jtype) jlogcallback "LogCallback" | |
255 | %typemap(jstype) jlogcallback "LogCallback" | |
256 | %typemap(javain) jlogcallback "$javainput" | |
257 | ||
258 | %extend sigrok::Context | |
259 | { | |
260 | void add_log_callback(JNIEnv *env, jlogcallback obj) | |
261 | { | |
262 | jclass obj_class = env->GetObjectClass(obj); | |
263 | jmethodID method = env->GetMethodID(obj_class, "run", | |
264 | "(Lorg/sigrok/core/classes/LogLevel;Ljava/lang/String;)V"); | |
265 | jclass LogLevel = (jclass) env->NewGlobalRef( | |
266 | env->FindClass("org/sigrok/core/classes/LogLevel")); | |
267 | jmethodID LogLevel_init = env->GetMethodID(LogLevel, "<init>", "(JZ)V"); | |
268 | jobject obj_ref = env->NewGlobalRef(obj); | |
269 | ||
270 | $self->set_log_callback([=] ( | |
271 | const sigrok::LogLevel *loglevel, | |
272 | std::string message) | |
273 | { | |
274 | jlong loglevel_addr; | |
275 | *(const sigrok::LogLevel **) &loglevel_addr = loglevel; | |
276 | jobject loglevel_obj = env->NewObject( | |
277 | LogLevel, LogLevel_init, loglevel_addr, false); | |
278 | jobject message_obj = env->NewStringUTF(message.c_str()); | |
279 | env->CallVoidMethod(obj_ref, method, loglevel_obj, message_obj); | |
280 | if (env->ExceptionCheck()) | |
281 | throw sigrok::Error(SR_ERR); | |
282 | }); | |
283 | } | |
284 | } | |
285 | ||
286 | /* Support Java datafeed callbacks. */ | |
287 | ||
90bd7656 ML |
288 | %typemap(javaimports) sigrok::Session |
289 | "import org.sigrok.core.interfaces.DatafeedCallback;" | |
290 | ||
9fcf4d0b ML |
291 | %inline { |
292 | typedef jobject jdatafeedcallback; | |
293 | } | |
294 | ||
295 | %typemap(jni) jdatafeedcallback "jdatafeedcallback" | |
296 | %typemap(jtype) jdatafeedcallback "DatafeedCallback" | |
297 | %typemap(jstype) jdatafeedcallback "DatafeedCallback" | |
298 | %typemap(javain) jdatafeedcallback "$javainput" | |
299 | ||
300 | %extend sigrok::Session | |
301 | { | |
302 | void add_datafeed_callback(JNIEnv *env, jdatafeedcallback obj) | |
303 | { | |
304 | jclass obj_class = env->GetObjectClass(obj); | |
305 | jmethodID method = env->GetMethodID(obj_class, "run", | |
306 | "(Lorg/sigrok/core/classes/Device;Lorg/sigrok/core/classes/Packet;)V"); | |
307 | jclass Device = (jclass) env->NewGlobalRef( | |
308 | env->FindClass("org/sigrok/core/classes/Device")); | |
309 | jmethodID Device_init = env->GetMethodID(Device, "<init>", "(JZ)V"); | |
310 | jclass Packet = (jclass) env->NewGlobalRef( | |
311 | env->FindClass("org/sigrok/core/classes/Packet")); | |
312 | jmethodID Packet_init = env->GetMethodID(Packet, "<init>", "(JZ)V"); | |
313 | jobject obj_ref = env->NewGlobalRef(obj); | |
314 | ||
315 | $self->add_datafeed_callback([=] ( | |
316 | std::shared_ptr<sigrok::Device> device, | |
317 | std::shared_ptr<sigrok::Packet> packet) | |
318 | { | |
319 | jlong device_addr; | |
320 | jlong packet_addr; | |
321 | *(std::shared_ptr<sigrok::Device> **) &device_addr = | |
322 | new std::shared_ptr<sigrok::Device>(device); | |
323 | *(std::shared_ptr<sigrok::Packet> **) &packet_addr = | |
324 | new std::shared_ptr<sigrok::Packet>(packet); | |
325 | jobject device_obj = env->NewObject( | |
326 | Device, Device_init, device_addr, true); | |
327 | jobject packet_obj = env->NewObject( | |
328 | Packet, Packet_init, packet_addr, true); | |
329 | env->CallVoidMethod(obj_ref, method, device_obj, packet_obj); | |
330 | if (env->ExceptionCheck()) | |
331 | throw sigrok::Error(SR_ERR); | |
332 | }); | |
333 | } | |
334 | } | |
335 | ||
bd4fda24 | 336 | %include "doc.i" |
062430a2 ML |
337 | |
338 | %define %attributevector(Class, Type, Name, Get) | |
339 | %attributeval(sigrok::Class, Type, Name, Get); | |
340 | %enddef | |
341 | ||
342 | %define %attributemap(Class, Type, Name, Get) | |
343 | %attributeval(sigrok::Class, Type, Name, Get); | |
344 | %enddef | |
345 | ||
7a36ceac ML |
346 | %define %enumextras(Class) |
347 | %enddef | |
348 | ||
b71356d6 UH |
349 | /* Ignore this for now, needs a fix. */ |
350 | %ignore sigrok::Context::create_analog_packet; | |
351 | ||
9fcf4d0b | 352 | %include "bindings/swig/classes.i" |
b71356d6 | 353 |