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