Removed the unneeded socket-raw plugin
[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 extern jclass *android_charonvpnservice_builder_class;
47
48 /**
49 * Attach the current thread to the JVM
50 *
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.
55 *
56 * @param env JNIEnv
57 */
58 void androidjni_attach_thread(JNIEnv **env);
59
60 /**
61 * Detach the current thread from the JVM
62 *
63 * Call this as soon as possible to ensure that local JNI references are freed.
64 */
65 void androidjni_detach_thread();
66
67 /**
68 * Handle exceptions thrown by a JNI call
69 *
70 * @param env JNIEnv
71 * @return TRUE if an exception was thrown
72 */
73 static inline bool androidjni_exception_occurred(JNIEnv *env)
74 {
75 if ((*env)->ExceptionOccurred(env))
76 { /* clear any exception, otherwise the VM is terminated */
77 (*env)->ExceptionDescribe(env);
78 (*env)->ExceptionClear(env);
79 return TRUE;
80 }
81 return FALSE;
82 }
83
84 /**
85 * Convert a Java string to a C string. Memory is allocated.
86 *
87 * @param env JNIEnv
88 * @param jstr Java string
89 * @return native C string (allocated)
90 */
91 static inline char *androidjni_convert_jstring(JNIEnv *env, jstring jstr)
92 {
93 char *str = NULL;
94 jsize len;
95
96 if (jstr)
97 {
98 len = (*env)->GetStringUTFLength(env, jstr);
99 str = malloc(len + 1);
100 (*env)->GetStringUTFRegion(env, jstr, 0, len, str);
101 str[len] = '\0';
102 }
103 return str;
104 }
105
106 #endif /** ANDROID_JNI_H_ @}*/