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