2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 * Hochschule fuer Technik Rapperswil
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * @defgroup android_jni android_jni
20 * @{ @ingroup libandroidbridge
23 #ifndef ANDROID_JNI_H_
24 #define ANDROID_JNI_H_
29 #define JNI_PACKAGE org_strongswan_android_logic
30 #define JNI_PACKAGE_STRING "org/strongswan/android/logic"
32 #define JNI_METHOD_PP(pack, klass, name, ret, ...) \
33 ret Java_##pack##_##klass##_##name(JNIEnv *env, jobject this, ##__VA_ARGS__)
35 #define JNI_METHOD_P(pack, klass, name, ret, ...) \
36 JNI_METHOD_PP(pack, klass, name, ret, ##__VA_ARGS__)
38 #define JNI_METHOD(klass, name, ret, ...) \
39 JNI_METHOD_P(JNI_PACKAGE, klass, name, ret, ##__VA_ARGS__)
43 * Initialized in JNI_OnLoad()
45 extern jclass
*android_charonvpnservice_class
;
46 extern jclass
*android_charonvpnservice_builder_class
;
49 * Attach the current thread to the JVM
51 * As local JNI references are not freed until the thread detaches
52 * androidjni_detach_thread() should be called as soon as possible.
53 * If it is not called a thread-local destructor ensures that the
54 * thread is at least detached as soon as it terminates.
58 void androidjni_attach_thread(JNIEnv
**env
);
61 * Detach the current thread from the JVM
63 * Call this as soon as possible to ensure that local JNI references are freed.
65 void androidjni_detach_thread();
68 * Handle exceptions thrown by a JNI call
71 * @return TRUE if an exception was thrown
73 static inline bool androidjni_exception_occurred(JNIEnv
*env
)
75 if ((*env
)->ExceptionOccurred(env
))
76 { /* clear any exception, otherwise the VM is terminated */
77 (*env
)->ExceptionDescribe(env
);
78 (*env
)->ExceptionClear(env
);
85 * Convert a Java string to a C string. Memory is allocated.
88 * @param jstr Java string
89 * @return native C string (allocated)
91 static inline char *androidjni_convert_jstring(JNIEnv
*env
, jstring jstr
)
96 len
= (*env
)->GetStringUTFLength(env
, jstr
);
97 str
= malloc(len
+ 1);
98 (*env
)->GetStringUTFRegion(env
, jstr
, 0, len
, str
);
103 #endif /** ANDROID_JNI_H_ @}*/