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
;
47 extern jclass
*android_simple_fetcher_class
;
50 * Currently known (supported) SDK versions
52 * see android.os.Build.VERSION_CODES for definitions
55 ANDROID_ICE_CREAM_SANDWICH
= 14,
56 ANDROID_ICE_CREAM_SANDWICH_MR1
= 15,
57 ANDROID_JELLY_BEAN
= 16,
58 ANDROID_JELLY_BEAN_MR1
= 17,
59 ANDROID_JELLY_BEAN_MR2
= 18,
60 } android_sdk_version_t
;
63 * The current SDK version of the Android framework
65 * see android.os.Build.VERSION.SDK_INT
67 extern android_sdk_version_t android_sdk_version
;
70 * Attach the current thread to the JVM
72 * As local JNI references are not freed until the thread detaches
73 * androidjni_detach_thread() should be called as soon as possible.
74 * If it is not called a thread-local destructor ensures that the
75 * thread is at least detached as soon as it terminates.
79 void androidjni_attach_thread(JNIEnv
**env
);
82 * Detach the current thread from the JVM
84 * Call this as soon as possible to ensure that local JNI references are freed.
86 void androidjni_detach_thread();
89 * Handle exceptions thrown by a JNI call
92 * @return TRUE if an exception was thrown
94 static inline bool androidjni_exception_occurred(JNIEnv
*env
)
96 if ((*env
)->ExceptionOccurred(env
))
97 { /* clear any exception, otherwise the VM is terminated */
98 (*env
)->ExceptionDescribe(env
);
99 (*env
)->ExceptionClear(env
);
106 * Convert a Java string to a C string. Memory is allocated.
109 * @param jstr Java string
110 * @return native C string (allocated)
112 static inline char *androidjni_convert_jstring(JNIEnv
*env
, jstring jstr
)
119 chars
= (*env
)->GetStringLength(env
, jstr
);
120 bytes
= (*env
)->GetStringUTFLength(env
, jstr
);
121 str
= malloc(bytes
+ 1);
122 (*env
)->GetStringUTFRegion(env
, jstr
, 0, chars
, str
);
129 * Converts the given Java byte array to a chunk
132 * @param jbytearray Java byte array
133 * @return allocated chunk
135 static inline chunk_t
chunk_from_byte_array(JNIEnv
*env
, jbyteArray jbytearray
)
139 chunk
= chunk_alloc((*env
)->GetArrayLength(env
, jbytearray
));
140 (*env
)->GetByteArrayRegion(env
, jbytearray
, 0, chunk
.len
, chunk
.ptr
);
145 * Converts the given chunk to a Java byte array
148 * @param chunk native chunk
149 * @return allocated Java byte array
151 static inline jbyteArray
byte_array_from_chunk(JNIEnv
*env
, chunk_t chunk
)
153 jbyteArray jbytearray
;
155 jbytearray
= (*env
)->NewByteArray(env
, chunk
.len
);
156 (*env
)->SetByteArrayRegion(env
, jbytearray
, 0, chunk
.len
, chunk
.ptr
);
160 #endif /** ANDROID_JNI_H_ @}*/