Merge branch 'master' into 'security-CKM' branch.
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOcStack.h
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2015 Intel Corporation.
5 * //
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 * //
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
11 * //
12 * //      http://www.apache.org/licenses/LICENSE-2.0
13 * //
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
19 * //
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 */
22 #include <jni.h>
23 #include <android/log.h>
24 #include "OCApi.h"
25
26 #ifndef _Included_org_iotivity_base_ocstack
27 #define _Included_org_iotivity_base_ocstack
28
29 #define TAG "OIC-JNI"
30
31 #define JNI_CURRENT_VERSION JNI_VERSION_1_6
32
33 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
34 #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
35 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
36
37 #define JNI_EXCEPTION 1000
38 #define JNI_NO_NATIVE_POINTER 1001
39 #define JNI_INVALID_VALUE 1002
40 #define JNI_NO_SUCH_KEY 1003
41
42 jobject getOcException(JNIEnv* env, const char* file, const char* functionName, const int line,
43     const int code, const char* message);
44 void throwOcException(JNIEnv* env, jobject ex);
45 #define GetOcException(code, message) getOcException (env,__FILE__,__func__,__LINE__,code,message)
46 #define ThrowOcException(code, message) throwOcException (env, GetOcException(code, message))
47
48 extern JavaVM* g_jvm;
49
50 extern jclass g_cls_Integer;
51 extern jclass g_cls_int1DArray;
52 extern jclass g_cls_int2DArray;
53 extern jclass g_cls_Double;
54 extern jclass g_cls_double1DArray;
55 extern jclass g_cls_double2DArray;
56 extern jclass g_cls_Boolean;
57 extern jclass g_cls_boolean1DArray;
58 extern jclass g_cls_boolean2DArray;
59 extern jclass g_cls_String;
60 extern jclass g_cls_String1DArray;
61 extern jclass g_cls_String2DArray;
62 extern jclass g_cls_LinkedList;
63 extern jclass g_cls_Map;
64 extern jclass g_cls_MapEntry;
65 extern jclass g_cls_Set;
66 extern jclass g_cls_Iterator;
67 extern jclass g_cls_HashMap;
68 extern jclass g_cls_OcException;
69 extern jclass g_cls_OcResource;
70 extern jclass g_cls_OcRepresentation;
71 extern jclass g_cls_OcRepresentation1DArray;
72 extern jclass g_cls_OcRepresentation2DArray;
73 extern jclass g_cls_OcResourceRequest;
74 extern jclass g_cls_OcResourceResponse;
75 extern jclass g_cls_OcResourceHandle;
76 extern jclass g_cls_OcPresenceHandle;
77 extern jclass g_cls_OcRequestHandle;
78 extern jclass g_cls_OcPresenceStatus;
79 extern jclass g_cls_OcHeaderOption;
80 extern jclass g_cls_ObservationInfo;
81 extern jclass g_cls_OcResourceIdentifier;
82
83 extern jmethodID g_mid_Integer_ctor;
84 extern jmethodID g_mid_Double_ctor;
85 extern jmethodID g_mid_Boolean_ctor;
86 extern jmethodID g_mid_LinkedList_ctor;
87 extern jmethodID g_mid_LinkedList_add_object;
88 extern jmethodID g_mid_Map_entrySet;
89 extern jmethodID g_mid_MapEntry_getKey;
90 extern jmethodID g_mid_MapEntry_getValue;
91 extern jmethodID g_mid_Set_iterator;
92 extern jmethodID g_mid_Iterator_hasNext;
93 extern jmethodID g_mid_Iterator_next;
94 extern jmethodID g_mid_HashMap_ctor;
95 extern jmethodID g_mid_HashMap_put;
96 extern jmethodID g_mid_OcException_ctor;
97 extern jmethodID g_mid_OcException_setNativeExceptionLocation;
98 extern jmethodID g_mid_OcResource_ctor;
99 extern jmethodID g_mid_OcRepresentation_N_ctor;
100 extern jmethodID g_mid_OcRepresentation_N_ctor_bool;
101 extern jmethodID g_mid_OcResourceRequest_N_ctor;
102 extern jmethodID g_mid_OcResourceResponse_N_ctor;
103 extern jmethodID g_mid_OcResourceHandle_N_ctor;
104 extern jmethodID g_mid_OcPresenceHandle_N_ctor;
105 extern jmethodID g_mid_OcRequestHandle_N_ctor;
106 extern jmethodID g_mid_OcHeaderOption_ctor;
107 extern jmethodID g_mid_OcHeaderOption_get_id;
108 extern jmethodID g_mid_OcHeaderOption_get_data;
109 extern jmethodID g_mid_ObservationInfo_N_ctor;
110 extern jmethodID g_mid_OcPresenceStatus_get;
111 extern jmethodID g_mid_OcResourceIdentifier_N_ctor;
112
113 typedef void(*RemoveListenerCallback)(JNIEnv* env, jobject jListener);
114
115 static jfieldID GetHandleField(JNIEnv *env, jobject jobj)
116 {
117     jclass cls = env->GetObjectClass(jobj);
118     return env->GetFieldID(cls, "mNativeHandle", "J");
119 }
120
121 template <typename T>
122 static T *GetHandle(JNIEnv *env, jobject jobj)
123 {
124     jlong handle = env->GetLongField(jobj, GetHandleField(env, jobj));
125     return reinterpret_cast<T *>(handle);
126 }
127
128 template <typename T>
129 static void SetHandle(JNIEnv *env, jobject jobj, T *type)
130 {
131     jlong handle = reinterpret_cast<jlong>(type);
132
133     env->SetLongField(jobj, GetHandleField(env, jobj), handle);
134 }
135
136 static JNIEnv* GetJNIEnv(jint& ret)
137 {
138     JNIEnv *env = NULL;
139
140     ret = g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION);
141     switch (ret) {
142     case JNI_OK:
143         return env;
144     case JNI_EDETACHED:
145         if (g_jvm->AttachCurrentThread(&env, NULL) < 0)
146         {
147             LOGE("Failed to get the environment");
148             return nullptr;
149         }
150         else
151         {
152             return env;
153         }
154
155     case JNI_EVERSION:
156         LOGE("JNI version not supported");
157     default:
158         LOGE("Failed to get the environment");
159         return nullptr;
160     }
161 }
162
163 static void DuplicateString(char ** targetString, std::string sourceString)
164 {
165     *targetString = new char[sourceString.length() + 1];
166     strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
167 }
168
169 #endif // _Included_org_iotivity_base_ocstack