Add APIs of cloud provisioning for Android and JNI layer
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / jni / JniJvm.cpp
1 /******************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20 #include "JniJvm.h"
21
22 JavaVM *g_jvm = NULL;
23
24 jclass g_cls_RemoteEnrollee = NULL;
25 jclass g_cls_ESException = NULL;
26 jclass g_cls_CloudProvisioningStatus = NULL;
27
28 jmethodID g_mid_RemoteEnrollee_ctor = NULL;
29 jmethodID g_mid_ESException_ctor = NULL;
30 jmethodID g_mid_CloudProvisioningStatus_ctor = NULL;
31
32 // JNI OnLoad
33 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
34 {
35     LOGI("JNI_OnLoad");
36     JNIEnv *env;
37     g_jvm = vm;
38
39     if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
40     {
41         LOGE("Failed to get the environment using GetEnv()");
42         return JNI_ERR;
43     }
44
45     jclass clazz = nullptr;
46
47     // Remote Enrollee
48     clazz = env->FindClass("org/iotivity/service/easysetup/mediator/RemoteEnrollee");
49     if (!clazz) return JNI_ERR;
50     g_cls_RemoteEnrollee = (jclass)env->NewGlobalRef(clazz);
51     env->DeleteLocalRef(clazz);
52
53     g_mid_RemoteEnrollee_ctor = env->GetMethodID(g_cls_RemoteEnrollee, "<init>",
54                                 "(J)V");
55     if (!g_mid_RemoteEnrollee_ctor) return JNI_ERR;
56
57     // ESException
58     clazz = env->FindClass("org/iotivity/service/easysetup/mediator/ESException");
59     if (!clazz) return JNI_ERR;
60     g_cls_ESException = (jclass)env->NewGlobalRef(clazz);
61     env->DeleteLocalRef(clazz);
62
63     g_mid_ESException_ctor = env->GetMethodID(g_cls_ESException, "<init>", "(Ljava/lang/String;)V");
64     if (!g_mid_ESException_ctor) return JNI_ERR;
65
66     // CloudProvisioningStatus
67     clazz = env->FindClass("org/iotivity/service/easysetup/mediator/CloudProvisioningStatus");
68     if (!clazz) return JNI_ERR;
69     g_cls_CloudProvisioningStatus = (jclass)env->NewGlobalRef(clazz);
70     env->DeleteLocalRef(clazz);
71
72     g_mid_CloudProvisioningStatus_ctor = env->GetMethodID(g_cls_CloudProvisioningStatus, "<init>",
73                                 "(II)V");
74     if (!g_mid_CloudProvisioningStatus_ctor) return JNI_ERR;
75
76     return JNI_CURRENT_VERSION;
77 }
78
79 //JNI OnUnload
80 JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
81 {
82     LOGI("JNI_OnUnload");
83     JNIEnv *env;
84
85     if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
86     {
87         LOGE("Failed to get the environment using GetEnv()");
88         return;
89     }
90     env->DeleteGlobalRef(g_cls_RemoteEnrollee);
91     env->DeleteGlobalRef(g_cls_ESException);
92 }