android: Add a simple HTTP(S) fetcher for CRLs
[strongswan.git] / src / frontends / android / app / src / main / 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 extern jclass *android_simple_fetcher_class;
48
49 /**
50 * Currently known (supported) SDK versions
51 *
52 * see android.os.Build.VERSION_CODES for definitions
53 */
54 typedef enum {
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;
61
62 /**
63 * The current SDK version of the Android framework
64 *
65 * see android.os.Build.VERSION.SDK_INT
66 */
67 extern android_sdk_version_t android_sdk_version;
68
69 /**
70 * Attach the current thread to the JVM
71 *
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.
76 *
77 * @param env JNIEnv
78 */
79 void androidjni_attach_thread(JNIEnv **env);
80
81 /**
82 * Detach the current thread from the JVM
83 *
84 * Call this as soon as possible to ensure that local JNI references are freed.
85 */
86 void androidjni_detach_thread();
87
88 /**
89 * Handle exceptions thrown by a JNI call
90 *
91 * @param env JNIEnv
92 * @return TRUE if an exception was thrown
93 */
94 static inline bool androidjni_exception_occurred(JNIEnv *env)
95 {
96 if ((*env)->ExceptionOccurred(env))
97 { /* clear any exception, otherwise the VM is terminated */
98 (*env)->ExceptionDescribe(env);
99 (*env)->ExceptionClear(env);
100 return TRUE;
101 }
102 return FALSE;
103 }
104
105 /**
106 * Convert a Java string to a C string. Memory is allocated.
107 *
108 * @param env JNIEnv
109 * @param jstr Java string
110 * @return native C string (allocated)
111 */
112 static inline char *androidjni_convert_jstring(JNIEnv *env, jstring jstr)
113 {
114 char *str = NULL;
115 jsize bytes, chars;
116
117 if (jstr)
118 {
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);
123 str[bytes] = '\0';
124 }
125 return str;
126 }
127
128 /**
129 * Converts the given Java byte array to a chunk
130 *
131 * @param env JNIEnv
132 * @param jbytearray Java byte array
133 * @return allocated chunk
134 */
135 static inline chunk_t chunk_from_byte_array(JNIEnv *env, jbyteArray jbytearray)
136 {
137 chunk_t chunk;
138
139 chunk = chunk_alloc((*env)->GetArrayLength(env, jbytearray));
140 (*env)->GetByteArrayRegion(env, jbytearray, 0, chunk.len, chunk.ptr);
141 return chunk;
142 }
143
144 /**
145 * Converts the given chunk to a Java byte array
146 *
147 * @param env JNIEnv
148 * @param chunk native chunk
149 * @return allocated Java byte array
150 */
151 static inline jbyteArray byte_array_from_chunk(JNIEnv *env, chunk_t chunk)
152 {
153 jbyteArray jbytearray;
154
155 jbytearray = (*env)->NewByteArray(env, chunk.len);
156 (*env)->SetByteArrayRegion(env, jbytearray, 0, chunk.len, chunk.ptr);
157 return jbytearray;
158 }
159
160 #endif /** ANDROID_JNI_H_ @}*/