06de766a0c7eee2357930f5e57218ca259fcfc91
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOcStack.cpp
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 "JniOcStack.h"
23 #include "OCApi.h"
24 #include "OCPlatform.h"
25 #include "OCRepresentation.h"
26 #include "JniUtils.h"
27
28 /**
29  * Macro to verify the validity of input argument.
30  *
31  * @param  arg  log level
32  */
33 #define VERIFY_VARIABLE_NULL(arg) \
34     if (nullptr == (arg)) \
35     { \
36         LOGE("invalid input"); \
37         return JNI_ERR; \
38     } \
39
40 JavaVM* g_jvm = nullptr;
41
42 jclass g_cls_Integer = nullptr;
43 jclass g_cls_int1DArray = nullptr;
44 jclass g_cls_int2DArray = nullptr;
45 jclass g_cls_Double = nullptr;
46 jclass g_cls_double1DArray = nullptr;
47 jclass g_cls_double2DArray = nullptr;
48 jclass g_cls_Boolean = nullptr;
49 jclass g_cls_boolean1DArray = nullptr;
50 jclass g_cls_boolean2DArray = nullptr;
51 jclass g_cls_String = nullptr;
52 jclass g_cls_String1DArray = nullptr;
53 jclass g_cls_String2DArray = nullptr;
54 jclass g_cls_LinkedList = nullptr;
55 jclass g_cls_Map = nullptr;
56 jclass g_cls_MapEntry = nullptr;
57 jclass g_cls_Set = nullptr;
58 jclass g_cls_Iterator = nullptr;
59 jclass g_cls_HashMap = nullptr;
60 jclass g_cls_OcException = nullptr;
61 jclass g_cls_OcResource = nullptr;
62 jclass g_cls_OcRepresentation = nullptr;
63 jclass g_cls_OcRepresentation1DArray = nullptr;
64 jclass g_cls_OcRepresentation2DArray = nullptr;
65 jclass g_cls_OcResourceRequest = nullptr;
66 jclass g_cls_OcResourceResponse = nullptr;
67 jclass g_cls_OcResourceHandle = nullptr;
68 jclass g_cls_OcPresenceHandle = nullptr;
69 jclass g_cls_OcRequestHandle = nullptr;
70 jclass g_cls_OcPresenceStatus = nullptr;
71 jclass g_cls_OcHeaderOption = nullptr;
72 jclass g_cls_ObservationInfo = nullptr;
73 jclass g_cls_OcResourceIdentifier = nullptr;
74 jclass g_cls_OcProvisionResult = nullptr;
75 jclass g_cls_OcSecureResource = nullptr;
76 jclass g_cls_OcOicSecAcl = nullptr;
77 jclass g_cls_OcOicSecAcl_ace = nullptr;
78 jclass g_cls_OcOicSecAcl_resr = nullptr;
79 jclass g_cls_OcOicSecAcl_validity = nullptr;
80 jclass g_cls_OcOicSecPdAcl = nullptr;
81 jclass g_cls_OcDirectPairDevice = nullptr;
82 #ifdef WITH_CLOUD
83 jclass g_cls_OcAccountManager = nullptr;
84 #endif
85
86 jmethodID g_mid_Integer_ctor = nullptr;
87 jmethodID g_mid_Double_ctor = nullptr;
88 jmethodID g_mid_Boolean_ctor = nullptr;
89 jmethodID g_mid_LinkedList_ctor = nullptr;
90 jmethodID g_mid_LinkedList_add_object = nullptr;
91 jmethodID g_mid_Map_entrySet = nullptr;
92 jmethodID g_mid_MapEntry_getKey = nullptr;
93 jmethodID g_mid_MapEntry_getValue = nullptr;
94 jmethodID g_mid_Set_iterator = nullptr;
95 jmethodID g_mid_Iterator_hasNext = nullptr;
96 jmethodID g_mid_Iterator_next = nullptr;
97 jmethodID g_mid_HashMap_ctor = nullptr;
98 jmethodID g_mid_HashMap_put = nullptr;
99 jmethodID g_mid_OcException_ctor = nullptr;
100 jmethodID g_mid_OcException_setNativeExceptionLocation = nullptr;
101 jmethodID g_mid_OcResource_ctor = nullptr;
102 jmethodID g_mid_OcRepresentation_N_ctor = nullptr;
103 jmethodID g_mid_OcRepresentation_N_ctor_bool = nullptr;
104 jmethodID g_mid_OcResourceRequest_N_ctor = nullptr;
105 jmethodID g_mid_OcResourceResponse_N_ctor = nullptr;
106 jmethodID g_mid_OcResourceHandle_N_ctor = nullptr;
107 jmethodID g_mid_OcPresenceHandle_N_ctor = nullptr;
108
109 jmethodID g_mid_OcRequestHandle_N_ctor = nullptr;
110 jmethodID g_mid_OcHeaderOption_ctor = nullptr;
111 jmethodID g_mid_OcHeaderOption_get_id = nullptr;
112 jmethodID g_mid_OcHeaderOption_get_data = nullptr;
113 jmethodID g_mid_ObservationInfo_N_ctor = nullptr;
114 jmethodID g_mid_OcPresenceStatus_get = nullptr;
115 jmethodID g_mid_OcResourceIdentifier_N_ctor = nullptr;
116 jmethodID g_mid_OcProvisionResult_ctor = nullptr;
117 jmethodID g_mid_OcSecureResource_ctor = nullptr;
118 jmethodID g_mid_OcDirectPairDevice_ctor = nullptr;
119 jmethodID g_mid_OcDirectPairDevice_dev_ctor = nullptr;
120 #ifdef WITH_CLOUD
121 jmethodID g_mid_OcAccountManager_ctor = nullptr;
122 #endif
123
124 jmethodID g_mid_OcOicSecPdAcl_get_resources_cnt = nullptr;
125 jmethodID g_mid_OcOicSecPdAcl_get_resources = nullptr;
126 jmethodID g_mid_OcOicSecPdAcl_get_permission = nullptr;
127 jmethodID g_mid_OcOicSecPdAcl_get_periods_cnt = nullptr;
128 jmethodID g_mid_OcOicSecPdAcl_get_periods = nullptr;
129 jmethodID g_mid_OcOicSecPdAcl_get_recurrences = nullptr;
130
131 jmethodID g_mid_OcOicSecAcl_get_aces = nullptr;
132 jmethodID g_mid_OcOicSecAcl_ace_get_subjectID = nullptr;
133 jmethodID g_mid_OcOicSecAcl_ace_get_permissions = nullptr;
134 jmethodID g_mid_OcOicSecAcl_ace_get_resources = nullptr;
135 jmethodID g_mid_OcOicSecAcl_ace_get_validities = nullptr;
136 jmethodID g_mid_OcOicSecAcl_resr_get_href = nullptr;
137 jmethodID g_mid_OcOicSecAcl_resr_get_rel = nullptr;
138 jmethodID g_mid_OcOicSecAcl_resr_get_types = nullptr;
139 jmethodID g_mid_OcOicSecAcl_resr_get_typeLen = nullptr;
140 jmethodID g_mid_OcOicSecAcl_resr_get_interfaces = nullptr;
141 jmethodID g_mid_OcOicSecAcl_validity_get_getPeriod = nullptr;
142 jmethodID g_mid_OcOicSecAcl_validity_get_recurrences = nullptr;
143 jmethodID g_mid_OcOicSecAcl_validity_get_recurrenceLen = nullptr;
144 jmethodID g_mid_OcOicSecAcl_resr_get_interfaceLen = nullptr;
145 jmethodID g_mid_OcOicSecAcl_get_rownerID = nullptr;
146
147 jobject getOcException(JNIEnv* env, const char* file, const char* functionName,
148     const int line, const int code, const char* message)
149 {
150     std::string codeStr = JniUtils::stackResultToStr(code);
151     if (codeStr.empty())
152     {
153         codeStr = JniUtils::stackResultToStr(JNI_INVALID_VALUE);
154     }
155     jobject ex = env->NewObject(g_cls_OcException,
156         g_mid_OcException_ctor,
157         env->NewStringUTF(codeStr.c_str()),
158         env->NewStringUTF(message));
159     if (!ex)
160     {
161         return nullptr;
162     }
163     env->CallVoidMethod(ex,
164         g_mid_OcException_setNativeExceptionLocation,
165         env->NewStringUTF(file),
166         env->NewStringUTF(functionName),
167         line);
168     if (env->ExceptionCheck())
169     {
170         return nullptr;
171     }
172     return ex;
173 }
174
175 void throwOcException(JNIEnv* env, jobject ex)
176 {
177     env->Throw((jthrowable)ex);
178 }
179
180 // JNI OnLoad
181 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
182 {
183     LOGI("JNI_OnLoad");
184     JNIEnv* env = nullptr;
185     g_jvm = vm;
186
187     if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
188     {
189         LOGE("Failed to get the environment using GetEnv()");
190         return JNI_ERR;
191     }
192     VERIFY_VARIABLE_NULL(env);
193
194     jclass clazz = nullptr;
195
196     //Integer
197     clazz = env->FindClass("java/lang/Integer");
198     VERIFY_VARIABLE_NULL(clazz);
199
200     g_cls_Integer = (jclass)env->NewGlobalRef(clazz);
201     env->DeleteLocalRef(clazz);
202
203     g_mid_Integer_ctor = env->GetMethodID(g_cls_Integer, "<init>", "(I)V");
204     VERIFY_VARIABLE_NULL(g_mid_Integer_ctor);
205
206     clazz = env->FindClass("[I");
207     VERIFY_VARIABLE_NULL(clazz);
208     g_cls_int1DArray = (jclass)env->NewGlobalRef(clazz);
209     env->DeleteLocalRef(clazz);
210
211     clazz = env->FindClass("[[I");
212     VERIFY_VARIABLE_NULL(clazz);
213     g_cls_int2DArray = (jclass)env->NewGlobalRef(clazz);
214     env->DeleteLocalRef(clazz);
215
216     //Double
217     clazz = env->FindClass("java/lang/Double");
218     VERIFY_VARIABLE_NULL(clazz);
219     g_cls_Double = (jclass)env->NewGlobalRef(clazz);
220     env->DeleteLocalRef(clazz);
221
222     g_mid_Double_ctor = env->GetMethodID(g_cls_Double, "<init>", "(D)V");
223     VERIFY_VARIABLE_NULL(g_mid_Double_ctor);
224
225     clazz = env->FindClass("[D");
226     VERIFY_VARIABLE_NULL(clazz);
227     g_cls_double1DArray = (jclass)env->NewGlobalRef(clazz);
228     env->DeleteLocalRef(clazz);
229
230     clazz = env->FindClass("[[D");
231     VERIFY_VARIABLE_NULL(clazz);
232     g_cls_double2DArray = (jclass)env->NewGlobalRef(clazz);
233     env->DeleteLocalRef(clazz);
234
235     //Boolean
236     clazz = env->FindClass("java/lang/Boolean");
237     VERIFY_VARIABLE_NULL(clazz);
238     g_cls_Boolean = (jclass)env->NewGlobalRef(clazz);
239     env->DeleteLocalRef(clazz);
240
241     g_mid_Boolean_ctor = env->GetMethodID(g_cls_Boolean, "<init>", "(Z)V");
242     VERIFY_VARIABLE_NULL(g_mid_Boolean_ctor);
243
244     clazz = env->FindClass("[Z");
245     VERIFY_VARIABLE_NULL(clazz);
246     g_cls_boolean1DArray = (jclass)env->NewGlobalRef(clazz);
247     env->DeleteLocalRef(clazz);
248
249     clazz = env->FindClass("[[Z");
250     VERIFY_VARIABLE_NULL(clazz);
251     g_cls_boolean2DArray = (jclass)env->NewGlobalRef(clazz);
252     env->DeleteLocalRef(clazz);
253
254     //String
255     clazz = env->FindClass("java/lang/String");
256     VERIFY_VARIABLE_NULL(clazz);
257     g_cls_String = (jclass)env->NewGlobalRef(clazz);
258     env->DeleteLocalRef(clazz);
259
260     clazz = env->FindClass("[Ljava/lang/String;");
261     VERIFY_VARIABLE_NULL(clazz);
262     g_cls_String1DArray = (jclass)env->NewGlobalRef(clazz);
263     env->DeleteLocalRef(clazz);
264
265     clazz = env->FindClass("[[Ljava/lang/String;");
266     VERIFY_VARIABLE_NULL(clazz);
267     g_cls_String2DArray = (jclass)env->NewGlobalRef(clazz);
268     env->DeleteLocalRef(clazz);
269
270     //LinkedList
271     clazz = env->FindClass("java/util/LinkedList");
272     VERIFY_VARIABLE_NULL(clazz);
273     g_cls_LinkedList = (jclass)env->NewGlobalRef(clazz);
274     env->DeleteLocalRef(clazz);
275
276     g_mid_LinkedList_ctor = env->GetMethodID(g_cls_LinkedList, "<init>", "()V");
277     VERIFY_VARIABLE_NULL(g_mid_LinkedList_ctor);
278
279     g_mid_LinkedList_add_object = env->GetMethodID(g_cls_LinkedList, "add", "(Ljava/lang/Object;)Z");
280     VERIFY_VARIABLE_NULL(g_mid_LinkedList_add_object);
281
282     //Map
283     clazz = env->FindClass("java/util/Map");
284     VERIFY_VARIABLE_NULL(clazz);
285     g_cls_Map = (jclass)env->NewGlobalRef(clazz);
286     env->DeleteLocalRef(clazz);
287
288     g_mid_Map_entrySet = env->GetMethodID(g_cls_Map, "entrySet", "()Ljava/util/Set;");
289     VERIFY_VARIABLE_NULL(g_mid_Map_entrySet);
290
291     //MapEntry
292     clazz = env->FindClass("java/util/Map$Entry");
293     VERIFY_VARIABLE_NULL(clazz);
294     g_cls_MapEntry = (jclass)env->NewGlobalRef(clazz);
295     env->DeleteLocalRef(clazz);
296
297     g_mid_MapEntry_getKey = env->GetMethodID(g_cls_MapEntry, "getKey", "()Ljava/lang/Object;");
298     VERIFY_VARIABLE_NULL(g_mid_MapEntry_getKey);
299     g_mid_MapEntry_getValue = env->GetMethodID(g_cls_MapEntry, "getValue", "()Ljava/lang/Object;");
300     VERIFY_VARIABLE_NULL(g_mid_MapEntry_getValue);
301
302     //Set
303     clazz = env->FindClass("java/util/Set");
304     VERIFY_VARIABLE_NULL(clazz);
305     g_cls_Set = (jclass)env->NewGlobalRef(clazz);
306     env->DeleteLocalRef(clazz);
307
308     g_mid_Set_iterator = env->GetMethodID(g_cls_Set, "iterator", "()Ljava/util/Iterator;");
309     VERIFY_VARIABLE_NULL(g_mid_Set_iterator);
310
311     //Iterator
312     clazz = env->FindClass("java/util/Iterator");
313     VERIFY_VARIABLE_NULL(clazz);
314     g_cls_Iterator = (jclass)env->NewGlobalRef(clazz);
315     env->DeleteLocalRef(clazz);
316
317     g_mid_Iterator_hasNext = env->GetMethodID(g_cls_Iterator, "hasNext", "()Z");
318     VERIFY_VARIABLE_NULL(g_mid_Iterator_hasNext);
319
320     g_mid_Iterator_next = env->GetMethodID(g_cls_Iterator, "next", "()Ljava/lang/Object;");
321     VERIFY_VARIABLE_NULL(g_mid_Iterator_next);
322
323     //HashMap
324     clazz = env->FindClass("java/util/HashMap");
325     VERIFY_VARIABLE_NULL(clazz);
326     g_cls_HashMap = (jclass)env->NewGlobalRef(clazz);
327     env->DeleteLocalRef(clazz);
328
329     g_mid_HashMap_ctor = env->GetMethodID(g_cls_HashMap, "<init>", "()V");
330     VERIFY_VARIABLE_NULL(g_mid_HashMap_ctor);
331
332     g_mid_HashMap_put = env->GetMethodID(g_cls_HashMap, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
333     VERIFY_VARIABLE_NULL(g_mid_HashMap_put);
334
335     //OcException
336     clazz = env->FindClass("org/iotivity/base/OcException");
337     VERIFY_VARIABLE_NULL(clazz);
338     g_cls_OcException = (jclass)env->NewGlobalRef(clazz);
339     env->DeleteLocalRef(clazz);
340
341     g_mid_OcException_ctor = env->GetMethodID(g_cls_OcException, "<init>", "(Ljava/lang/String;Ljava/lang/String;)V");
342     VERIFY_VARIABLE_NULL(g_mid_OcException_ctor);
343
344     g_mid_OcException_setNativeExceptionLocation = env->GetMethodID(g_cls_OcException, "setNativeExceptionLocation",
345         "(Ljava/lang/String;""Ljava/lang/String;""I)V");
346     VERIFY_VARIABLE_NULL(g_mid_OcException_setNativeExceptionLocation);
347
348     //OcResource
349     clazz = env->FindClass("org/iotivity/base/OcResource");
350     VERIFY_VARIABLE_NULL(clazz);
351     g_cls_OcResource = (jclass)env->NewGlobalRef(clazz);
352     env->DeleteLocalRef(clazz);
353
354     g_mid_OcResource_ctor = env->GetMethodID(g_cls_OcResource, "<init>", "(J)V");
355     VERIFY_VARIABLE_NULL(g_mid_OcResource_ctor);
356
357     //OcRepresentation
358     clazz = env->FindClass("org/iotivity/base/OcRepresentation");
359     VERIFY_VARIABLE_NULL(clazz);
360     g_cls_OcRepresentation = (jclass)env->NewGlobalRef(clazz);
361     env->DeleteLocalRef(clazz);
362
363     g_mid_OcRepresentation_N_ctor = env->GetMethodID(g_cls_OcRepresentation, "<init>", "(J)V");
364     VERIFY_VARIABLE_NULL(g_mid_OcRepresentation_N_ctor);
365
366     g_mid_OcRepresentation_N_ctor_bool = env->GetMethodID(g_cls_OcRepresentation, "<init>", "(JZ)V");
367     VERIFY_VARIABLE_NULL(g_mid_OcRepresentation_N_ctor_bool);
368
369     clazz = env->FindClass("[Lorg/iotivity/base/OcRepresentation;");
370     VERIFY_VARIABLE_NULL(clazz);
371     g_cls_OcRepresentation1DArray = (jclass)env->NewGlobalRef(clazz);
372     env->DeleteLocalRef(clazz);
373
374     clazz = env->FindClass("[[Lorg/iotivity/base/OcRepresentation;");
375     VERIFY_VARIABLE_NULL(clazz);
376     g_cls_OcRepresentation2DArray = (jclass)env->NewGlobalRef(clazz);
377     env->DeleteLocalRef(clazz);
378
379     //HeaderOptions
380     clazz = env->FindClass("org/iotivity/base/OcHeaderOption");
381     VERIFY_VARIABLE_NULL(clazz);
382     g_cls_OcHeaderOption = (jclass)env->NewGlobalRef(clazz);
383     env->DeleteLocalRef(clazz);
384     g_mid_OcHeaderOption_ctor = env->GetMethodID(g_cls_OcHeaderOption, "<init>", "(ILjava/lang/String;)V");
385     VERIFY_VARIABLE_NULL(g_mid_OcHeaderOption_ctor);
386
387     g_mid_OcHeaderOption_get_id = env->GetMethodID(g_cls_OcHeaderOption, "getOptionId", "()I");
388     VERIFY_VARIABLE_NULL(g_mid_OcHeaderOption_get_id);
389
390     g_mid_OcHeaderOption_get_data = env->GetMethodID(g_cls_OcHeaderOption, "getOptionData", "()Ljava/lang/String;");
391     VERIFY_VARIABLE_NULL(g_mid_OcHeaderOption_get_data);
392
393     //OcResourceRequest
394     clazz = env->FindClass("org/iotivity/base/OcResourceRequest");
395     VERIFY_VARIABLE_NULL(clazz);
396     g_cls_OcResourceRequest = (jclass)env->NewGlobalRef(clazz);
397     env->DeleteLocalRef(clazz);
398
399     g_mid_OcResourceRequest_N_ctor = env->GetMethodID(g_cls_OcResourceRequest, "<init>", "(J)V");
400     VERIFY_VARIABLE_NULL(g_mid_OcResourceRequest_N_ctor);
401
402     //OcResourceResponse
403     clazz = env->FindClass("org/iotivity/base/OcResourceResponse");
404     VERIFY_VARIABLE_NULL(clazz);
405     g_cls_OcResourceResponse = (jclass)env->NewGlobalRef(clazz);
406     env->DeleteLocalRef(clazz);
407
408     g_mid_OcResourceResponse_N_ctor = env->GetMethodID(g_cls_OcResourceResponse, "<init>", "(J)V");
409     VERIFY_VARIABLE_NULL(g_mid_OcResourceResponse_N_ctor);
410
411     //OcResourceHandle
412     clazz = env->FindClass("org/iotivity/base/OcResourceHandle");
413     VERIFY_VARIABLE_NULL(clazz);
414     g_cls_OcResourceHandle = (jclass)env->NewGlobalRef(clazz);
415     env->DeleteLocalRef(clazz);
416     g_mid_OcResourceHandle_N_ctor = env->GetMethodID(g_cls_OcResourceHandle, "<init>", "(J)V");
417     VERIFY_VARIABLE_NULL(g_mid_OcResourceHandle_N_ctor);
418
419     //OcPresenceHandle
420     clazz = env->FindClass("org/iotivity/base/OcPresenceHandle");
421     VERIFY_VARIABLE_NULL(clazz);
422     g_cls_OcPresenceHandle = (jclass)env->NewGlobalRef(clazz);
423     env->DeleteLocalRef(clazz);
424     g_mid_OcPresenceHandle_N_ctor = env->GetMethodID(g_cls_OcPresenceHandle, "<init>", "(J)V");
425     VERIFY_VARIABLE_NULL(g_mid_OcPresenceHandle_N_ctor);
426
427     //OcRequestHandle
428     clazz = env->FindClass("org/iotivity/base/OcRequestHandle");
429     VERIFY_VARIABLE_NULL(clazz);
430     g_cls_OcRequestHandle = (jclass)env->NewGlobalRef(clazz);
431     env->DeleteLocalRef(clazz);
432     g_mid_OcRequestHandle_N_ctor = env->GetMethodID(g_cls_OcRequestHandle, "<init>", "(J)V");
433     VERIFY_VARIABLE_NULL(g_mid_OcRequestHandle_N_ctor);
434
435     //OcPresenceStatus
436     clazz = env->FindClass("org/iotivity/base/OcPresenceStatus");
437     VERIFY_VARIABLE_NULL(clazz);
438     g_cls_OcPresenceStatus = (jclass)env->NewGlobalRef(clazz);
439     env->DeleteLocalRef(clazz);
440     g_mid_OcPresenceStatus_get = env->GetStaticMethodID(g_cls_OcPresenceStatus, "get",
441         "(Ljava/lang/String;)Lorg/iotivity/base/OcPresenceStatus;");
442     VERIFY_VARIABLE_NULL(g_mid_OcPresenceStatus_get);
443
444     //ObservationInfo
445     clazz = env->FindClass("org/iotivity/base/ObservationInfo");
446     VERIFY_VARIABLE_NULL(clazz);
447     g_cls_ObservationInfo = (jclass)env->NewGlobalRef(clazz);
448     env->DeleteLocalRef(clazz);
449     g_mid_ObservationInfo_N_ctor = env->GetMethodID(g_cls_ObservationInfo, "<init>", "(IB)V");
450     VERIFY_VARIABLE_NULL(g_mid_ObservationInfo_N_ctor);
451
452     clazz = env->FindClass("org/iotivity/base/OcResourceIdentifier");
453     VERIFY_VARIABLE_NULL(clazz);
454     g_cls_OcResourceIdentifier = (jclass)env->NewGlobalRef(clazz);
455     env->DeleteLocalRef(clazz);
456     g_mid_OcResourceIdentifier_N_ctor = env->GetMethodID(g_cls_OcResourceIdentifier, "<init>", "(J)V");
457     VERIFY_VARIABLE_NULL(g_mid_OcResourceIdentifier_N_ctor);
458
459     //OcSecureResource
460     clazz = env->FindClass("org/iotivity/base/OcSecureResource");
461     VERIFY_VARIABLE_NULL(clazz);
462     g_cls_OcSecureResource =  (jclass)env->NewGlobalRef(clazz);
463     env->DeleteLocalRef(clazz);
464     g_mid_OcSecureResource_ctor = env->GetMethodID(g_cls_OcSecureResource, "<init>", "(J)V");
465     VERIFY_VARIABLE_NULL(g_mid_OcSecureResource_ctor);
466
467     //ProvisionResult
468     clazz = env->FindClass("org/iotivity/base/ProvisionResult");
469     VERIFY_VARIABLE_NULL(clazz);
470     g_cls_OcProvisionResult =  (jclass)env->NewGlobalRef(clazz);
471     env->DeleteLocalRef(clazz);
472     g_mid_OcProvisionResult_ctor = env->GetMethodID(g_cls_OcProvisionResult, "<init>", "(Ljava/lang/String;I)V");
473     VERIFY_VARIABLE_NULL(g_mid_OcProvisionResult_ctor);
474
475     //OcDirectPairDevice
476     clazz = env->FindClass("org/iotivity/base/OcDirectPairDevice");
477     VERIFY_VARIABLE_NULL(clazz);
478     g_cls_OcDirectPairDevice =  (jclass)env->NewGlobalRef(clazz);
479     g_mid_OcDirectPairDevice_ctor = env->GetMethodID(g_cls_OcDirectPairDevice, "<init>", "(J)V");
480     VERIFY_VARIABLE_NULL(g_mid_OcDirectPairDevice_ctor);
481
482     g_mid_OcDirectPairDevice_dev_ctor = env->GetMethodID(g_cls_OcDirectPairDevice, "<init>", "(Ljava/lang/String;)V");
483     VERIFY_VARIABLE_NULL(g_mid_OcDirectPairDevice_dev_ctor);
484     env->DeleteLocalRef(clazz);
485
486 #ifdef WITH_CLOUD
487     //OcAccountManager
488     clazz = env->FindClass("org/iotivity/base/OcAccountManager");
489     VERIFY_VARIABLE_NULL(clazz);
490     g_cls_OcAccountManager = (jclass)env->NewGlobalRef(clazz);
491     env->DeleteLocalRef(clazz);
492
493     g_mid_OcAccountManager_ctor = env->GetMethodID(g_cls_OcAccountManager, "<init>", "(J)V");
494     VERIFY_VARIABLE_NULL(g_mid_OcAccountManager_ctor);
495 #endif
496
497     //OicSecAcl
498     clazz = env->FindClass("org/iotivity/base/OicSecAcl");
499     VERIFY_VARIABLE_NULL(clazz);
500     g_cls_OcOicSecAcl =  (jclass)env->NewGlobalRef(clazz);
501     env->DeleteLocalRef(clazz);
502
503     g_mid_OcOicSecAcl_get_rownerID = env->GetMethodID(g_cls_OcOicSecAcl, "getRownerID", "()Ljava/lang/String;");
504     VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_get_rownerID);
505
506     g_mid_OcOicSecAcl_get_aces = env->GetMethodID(g_cls_OcOicSecAcl, "getOicSecAces", "()[Lorg/iotivity/base/OicSecAce;");
507     VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_get_aces);
508
509     //OicSecAce
510     clazz = env->FindClass("org/iotivity/base/OicSecAce");
511     VERIFY_VARIABLE_NULL(clazz);
512     g_cls_OcOicSecAcl_ace =  (jclass)env->NewGlobalRef(clazz);
513     env->DeleteLocalRef(clazz);
514
515     g_mid_OcOicSecAcl_ace_get_subjectID = env->GetMethodID(g_cls_OcOicSecAcl_ace, "getSubjectID","()Ljava/lang/String;");
516     VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_ace_get_subjectID);
517
518     g_mid_OcOicSecAcl_ace_get_permissions = env->GetMethodID(g_cls_OcOicSecAcl_ace, "getPermission","()I");
519     VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_ace_get_permissions);
520
521     g_mid_OcOicSecAcl_ace_get_resources = env->GetMethodID(g_cls_OcOicSecAcl_ace, "getResources","()[Lorg/iotivity/base/OicSecResr;");
522     VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_ace_get_resources);
523
524     g_mid_OcOicSecAcl_ace_get_validities = env->GetMethodID(g_cls_OcOicSecAcl_ace, "getValidities","()[Lorg/iotivity/base/OicSecValidity;");
525     VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_ace_get_validities);
526
527     //OicSecResr
528     clazz = env->FindClass("org/iotivity/base/OicSecResr");
529     VERIFY_VARIABLE_NULL(clazz);
530     g_cls_OcOicSecAcl_resr =  (jclass)env->NewGlobalRef(clazz);
531     env->DeleteLocalRef(clazz);
532
533     g_mid_OcOicSecAcl_resr_get_href = env->GetMethodID(g_cls_OcOicSecAcl_resr, "getHref","()Ljava/lang/String;");
534     VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_resr_get_href);
535
536     g_mid_OcOicSecAcl_resr_get_rel = env->GetMethodID(g_cls_OcOicSecAcl_resr, "getRel","()Ljava/lang/String;");
537     VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_resr_get_rel);
538
539     g_mid_OcOicSecAcl_resr_get_types = env->GetMethodID(g_cls_OcOicSecAcl_resr, "getTypes","(I)Ljava/lang/String;");
540     VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_resr_get_types);
541
542     g_mid_OcOicSecAcl_resr_get_typeLen = env->GetMethodID(g_cls_OcOicSecAcl_resr, "getTypeLen","()I");
543     VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_resr_get_typeLen);
544
545     g_mid_OcOicSecAcl_resr_get_interfaces = env->GetMethodID(g_cls_OcOicSecAcl_resr, "getInterfaces","(I)Ljava/lang/String;");
546     VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_resr_get_interfaces);
547
548     g_mid_OcOicSecAcl_resr_get_interfaceLen = env->GetMethodID(g_cls_OcOicSecAcl_resr, "getInterfaceLen","()I");
549     VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_resr_get_interfaceLen);
550
551     //OicSecAcl$OicSecValidity
552     clazz = env->FindClass("org/iotivity/base/OicSecValidity");
553     VERIFY_VARIABLE_NULL(clazz);
554     g_cls_OcOicSecAcl_validity =  (jclass)env->NewGlobalRef(clazz);
555     env->DeleteLocalRef(clazz);
556
557     g_mid_OcOicSecAcl_validity_get_getPeriod = env->GetMethodID(g_cls_OcOicSecAcl_validity, "getPeriod","()Ljava/lang/String;");
558     VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_validity_get_getPeriod);
559
560     g_mid_OcOicSecAcl_validity_get_recurrences = env->GetMethodID(g_cls_OcOicSecAcl_validity, "getRecurrences","(I)Ljava/lang/String;");
561     VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_validity_get_recurrences);
562
563     g_mid_OcOicSecAcl_validity_get_recurrenceLen = env->GetMethodID(g_cls_OcOicSecAcl_validity, "getRecurrenceLen","()I");
564     VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_validity_get_recurrenceLen);
565
566     //OicSecPdAcl
567     clazz = env->FindClass("org/iotivity/base/OicSecPdAcl");
568     VERIFY_VARIABLE_NULL(clazz);
569     g_cls_OcOicSecPdAcl =  (jclass)env->NewGlobalRef(clazz);
570     env->DeleteLocalRef(clazz);
571
572     g_mid_OcOicSecPdAcl_get_resources_cnt = env->GetMethodID(g_cls_OcOicSecPdAcl, "getResourcesCount", "()I");
573     VERIFY_VARIABLE_NULL(g_mid_OcOicSecPdAcl_get_resources_cnt);
574
575     g_mid_OcOicSecPdAcl_get_resources = env->GetMethodID(g_cls_OcOicSecPdAcl, "getResources", "(I)Ljava/lang/String;");
576     VERIFY_VARIABLE_NULL(g_mid_OcOicSecPdAcl_get_resources);
577
578     g_mid_OcOicSecPdAcl_get_permission = env->GetMethodID(g_cls_OcOicSecPdAcl, "getPermission", "()I");
579     VERIFY_VARIABLE_NULL(g_mid_OcOicSecPdAcl_get_permission);
580
581     g_mid_OcOicSecPdAcl_get_periods_cnt = env->GetMethodID(g_cls_OcOicSecPdAcl, "getPeriodsCount", "()I");
582     VERIFY_VARIABLE_NULL(g_mid_OcOicSecPdAcl_get_periods_cnt);
583
584     g_mid_OcOicSecPdAcl_get_periods = env->GetMethodID(g_cls_OcOicSecPdAcl, "getPeriods", "(I)Ljava/lang/String;");
585     VERIFY_VARIABLE_NULL(g_mid_OcOicSecPdAcl_get_periods);
586
587     g_mid_OcOicSecPdAcl_get_recurrences = env->GetMethodID(g_cls_OcOicSecPdAcl, "getRecurrences", "(I)Ljava/lang/String;");
588     VERIFY_VARIABLE_NULL(g_mid_OcOicSecPdAcl_get_recurrences);
589
590     return JNI_CURRENT_VERSION;
591 }
592
593 JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
594 {
595     LOGI("JNI_OnUnload");
596     JNIEnv* env = nullptr;
597
598     if (vm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
599     {
600         LOGE("Failed to get the environment using GetEnv()");
601         return;
602     }
603
604     if (env)
605     {
606         env->DeleteGlobalRef(g_cls_Integer);
607         env->DeleteGlobalRef(g_cls_int1DArray);
608         env->DeleteGlobalRef(g_cls_int2DArray);
609         env->DeleteGlobalRef(g_cls_Double);
610         env->DeleteGlobalRef(g_cls_double1DArray);
611         env->DeleteGlobalRef(g_cls_double2DArray);
612         env->DeleteGlobalRef(g_cls_Boolean);
613         env->DeleteGlobalRef(g_cls_boolean1DArray);
614         env->DeleteGlobalRef(g_cls_boolean2DArray);
615         env->DeleteGlobalRef(g_cls_String);
616         env->DeleteGlobalRef(g_cls_String1DArray);
617         env->DeleteGlobalRef(g_cls_String2DArray);
618         env->DeleteGlobalRef(g_cls_LinkedList);
619         env->DeleteGlobalRef(g_cls_Map);
620         env->DeleteGlobalRef(g_cls_MapEntry);
621         env->DeleteGlobalRef(g_cls_Set);
622         env->DeleteGlobalRef(g_cls_Iterator);
623         env->DeleteGlobalRef(g_cls_HashMap);
624         env->DeleteGlobalRef(g_cls_OcResource);
625         env->DeleteGlobalRef(g_cls_OcException);
626         env->DeleteGlobalRef(g_cls_OcRepresentation);
627         env->DeleteGlobalRef(g_cls_OcRepresentation1DArray);
628         env->DeleteGlobalRef(g_cls_OcRepresentation2DArray);
629         env->DeleteGlobalRef(g_cls_OcResourceRequest);
630         env->DeleteGlobalRef(g_cls_OcResourceResponse);
631         env->DeleteGlobalRef(g_cls_OcResourceHandle);
632         env->DeleteGlobalRef(g_cls_OcPresenceHandle);
633         env->DeleteGlobalRef(g_cls_OcRequestHandle);
634         env->DeleteGlobalRef(g_cls_OcPresenceStatus);
635         env->DeleteGlobalRef(g_cls_OcHeaderOption);
636         env->DeleteGlobalRef(g_cls_ObservationInfo);
637         env->DeleteGlobalRef(g_cls_OcResourceIdentifier);
638         env->DeleteGlobalRef(g_cls_OcSecureResource);
639         env->DeleteGlobalRef(g_cls_OcProvisionResult);
640         env->DeleteGlobalRef(g_cls_OcDirectPairDevice);
641 #ifdef WITH_CLOUD
642         env->DeleteGlobalRef(g_cls_OcAccountManager);
643 #endif
644         env->DeleteGlobalRef(g_cls_OcOicSecAcl);
645         env->DeleteGlobalRef(g_cls_OcOicSecAcl_ace);
646         env->DeleteGlobalRef(g_cls_OcOicSecAcl_resr);
647         env->DeleteGlobalRef(g_cls_OcOicSecAcl_validity);
648     }
649 }