427c641b4668a9757b12322cfc483bad2b9e9410
[strongswan.git] / src / frontends / android / jni / libandroidbridge / android_jni.h
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 * Hochschule fuer Technik Rapperswil
6 *
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>.
11 *
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
15 * for more details.
16 */
17
18 /**
19 * @defgroup android_jni android_jni
20 * @{ @ingroup libandroidbridge
21 */
22
23 #ifndef ANDROID_JNI_H_
24 #define ANDROID_JNI_H_
25
26 #include <jni.h>
27 #include <library.h>
28
29 #define JNI_PACKAGE org_strongswan_android_logic
30 #define JNI_PACKAGE_STRING "org/strongswan/android/logic"
31
32 #define JNI_METHOD_PP(pack, klass, name, ret, ...) \
33 ret Java_##pack##_##klass##_##name(JNIEnv *env, jobject this, ##__VA_ARGS__)
34
35 #define JNI_METHOD_P(pack, klass, name, ret, ...) \
36 JNI_METHOD_PP(pack, klass, name, ret, ##__VA_ARGS__)
37
38 #define JNI_METHOD(klass, name, ret, ...) \
39 JNI_METHOD_P(JNI_PACKAGE, klass, name, ret, ##__VA_ARGS__)
40
41 /**
42 * Java classes
43 * Initialized in JNI_OnLoad()
44 */
45 extern jclass *android_charonvpnservice_class;
46
47 /**
48 * Attach the current thread to the JVM
49 *
50 * As local JNI references are not freed until the thread detaches
51 * androidjni_detach_thread() should be called as soon as possible.
52 * If it is not called a thread-local destructor ensures that the
53 * thread is at least detached as soon as it terminates.
54 *
55 * @param env JNIEnv
56 */
57 void androidjni_attach_thread(JNIEnv **env);
58
59 /**
60 * Detach the current thread from the JVM
61 *
62 * Call this as soon as possible to ensure that local JNI references are freed.
63 */
64 void androidjni_detach_thread();
65
66 /**
67 * Handle exceptions thrown by a JNI call
68 *
69 * @param env JNIEnv
70 * @return TRUE if an exception was thrown
71 */
72 static inline bool androidjni_exception_occurred(JNIEnv *env)
73 {
74 if ((*env)->ExceptionOccurred(env))
75 { /* clear any exception, otherwise the VM is terminated */
76 (*env)->ExceptionDescribe(env);
77 (*env)->ExceptionClear(env);
78 return TRUE;
79 }
80 return FALSE;
81 }
82
83 #endif /** ANDROID_JNI_H_ @}*/