android: Add a custom kernel-net implementation to replace kernel-netlink
[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 * Currently known (supported) SDK versions
50 *
51 * see android.os.Build.VERSION_CODES for definitions
52 */
53 typedef enum {
54 ANDROID_ICE_CREAM_SANDWICH = 14,
55 ANDROID_ICE_CREAM_SANDWICH_MR1 = 15,
56 ANDROID_JELLY_BEAN = 16,
57 } android_sdk_version_t;
58
59 /**
60 * The current SDK version of the Android framework
61 *
62 * see android.os.Build.VERSION.SDK_INT
63 */
64 extern android_sdk_version_t android_sdk_version;
65
66 /**
67 * Attach the current thread to the JVM
68 *
69 * As local JNI references are not freed until the thread detaches
70 * androidjni_detach_thread() should be called as soon as possible.
71 * If it is not called a thread-local destructor ensures that the
72 * thread is at least detached as soon as it terminates.
73 *
74 * @param env JNIEnv
75 */
76 void androidjni_attach_thread(JNIEnv **env);
77
78 /**
79 * Detach the current thread from the JVM
80 *
81 * Call this as soon as possible to ensure that local JNI references are freed.
82 */
83 void androidjni_detach_thread();
84
85 /**
86 * Handle exceptions thrown by a JNI call
87 *
88 * @param env JNIEnv
89 * @return TRUE if an exception was thrown
90 */
91 static inline bool androidjni_exception_occurred(JNIEnv *env)
92 {
93 if ((*env)->ExceptionOccurred(env))
94 { /* clear any exception, otherwise the VM is terminated */
95 (*env)->ExceptionDescribe(env);
96 (*env)->ExceptionClear(env);
97 return TRUE;
98 }
99 return FALSE;
100 }
101
102 /**
103 * Convert a Java string to a C string. Memory is allocated.
104 *
105 * @param env JNIEnv
106 * @param jstr Java string
107 * @return native C string (allocated)
108 */
109 static inline char *androidjni_convert_jstring(JNIEnv *env, jstring jstr)
110 {
111 char *str = NULL;
112 jsize bytes, chars;
113
114 if (jstr)
115 {
116 chars = (*env)->GetStringLength(env, jstr);
117 bytes = (*env)->GetStringUTFLength(env, jstr);
118 str = malloc(bytes + 1);
119 (*env)->GetStringUTFRegion(env, jstr, 0, chars, str);
120 str[bytes] = '\0';
121 }
122 return str;
123 }
124
125 /**
126 * Converts the given Java byte array to a chunk
127 *
128 * @param env JNIEnv
129 * @param jbytearray Java byte array
130 * @return allocated chunk
131 */
132 static inline chunk_t chunk_from_byte_array(JNIEnv *env, jbyteArray jbytearray)
133 {
134 chunk_t chunk;
135
136 chunk = chunk_alloc((*env)->GetArrayLength(env, jbytearray));
137 (*env)->GetByteArrayRegion(env, jbytearray, 0, chunk.len, chunk.ptr);
138 return chunk;
139 }
140
141 /**
142 * Converts the given chunk to a Java byte array
143 *
144 * @param env JNIEnv
145 * @param chunk native chunk
146 * @return allocated Java byte array
147 */
148 static inline jbyteArray byte_array_from_chunk(JNIEnv *env, chunk_t chunk)
149 {
150 jbyteArray jbytearray;
151
152 jbytearray = (*env)->NewByteArray(env, chunk.len);
153 (*env)->SetByteArrayRegion(env, jbytearray, 0, chunk.len, chunk.ptr);
154 return jbytearray;
155 }
156
157 #endif /** ANDROID_JNI_H_ @}*/