replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / EasySetupCore / src / main / 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_EnrolleeStatus = NULL;
27 jclass g_cls_EnrolleeConf = NULL;
28 jclass g_cls_getEnrolleeStatus = NULL;
29 jclass g_cls_getConfigurationStatus = NULL;
30 jclass g_cls_SecurityProvisioningStatus = NULL;
31 jclass g_cls_DevicePropProvisioningStatus = NULL;
32 jclass g_cls_CloudPropProvisioningStatus = NULL;
33 jclass g_cls_Integer = NULL;
34 jclass g_cls_OcRepresentation = NULL;
35
36 jmethodID g_mid_RemoteEnrollee_ctor = NULL;
37 jmethodID g_mid_ESException_ctor = NULL;
38 jmethodID g_mid_EnrolleeStatus_ctor = NULL;
39 jmethodID g_mid_EnrolleeConf_ctor = NULL;
40 jmethodID g_mid_getEnrolleeStatus_ctor = NULL;
41 jmethodID g_mid_getConfigurationStatus_ctor = NULL;
42 jmethodID g_mid_SecurityProvisioningStatus_ctor = NULL;
43 jmethodID g_mid_DevicePropProvisioningStatus_ctor = NULL;
44 jmethodID g_mid_CloudPropProvisioningStatus_ctor = NULL;
45 jmethodID g_mid_Integer_ctor = NULL;
46 jmethodID g_mid_OcRepresentation_N_ctor_bool = NULL;
47
48 // JNI OnLoad
49 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
50 {
51     ES_LOGI("JNI_OnLoad");
52     JNIEnv *env;
53     g_jvm = vm;
54
55     if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
56     {
57         ES_LOGE("Failed to get the environment using GetEnv()");
58         return JNI_ERR;
59     }
60
61     jclass clazz = nullptr;
62
63     // Remote Enrollee
64     clazz = env->FindClass("org/iotivity/service/easysetup/mediator/RemoteEnrollee");
65     if (!clazz) return JNI_ERR;
66     g_cls_RemoteEnrollee = (jclass)env->NewGlobalRef(clazz);
67     env->DeleteLocalRef(clazz);
68
69     g_mid_RemoteEnrollee_ctor = env->GetMethodID(g_cls_RemoteEnrollee, "<init>",
70                                 "(J)V");
71     if (!g_mid_RemoteEnrollee_ctor) return JNI_ERR;
72
73     // ESException
74     clazz = env->FindClass("org/iotivity/service/easysetup/mediator/ESException");
75     if (!clazz) return JNI_ERR;
76     g_cls_ESException = (jclass)env->NewGlobalRef(clazz);
77     env->DeleteLocalRef(clazz);
78
79     g_mid_ESException_ctor = env->GetMethodID(g_cls_ESException, "<init>", "(Ljava/lang/String;)V");
80     if (!g_mid_ESException_ctor) return JNI_ERR;
81
82     // EnrolleeStatus
83     clazz = env->FindClass("org/iotivity/service/easysetup/mediator/EnrolleeStatus");
84     if (!clazz) return JNI_ERR;
85
86     g_cls_EnrolleeStatus = (jclass)env->NewGlobalRef(clazz);
87     env->DeleteLocalRef(clazz);
88
89     g_mid_EnrolleeStatus_ctor = env->GetMethodID(g_cls_EnrolleeStatus, "<init>",
90                                 "(Lorg/iotivity/base/OcRepresentation;)V");
91     if (!g_mid_EnrolleeStatus_ctor) return JNI_ERR;
92
93     // EnrolleeConf
94     clazz = env->FindClass("org/iotivity/service/easysetup/mediator/EnrolleeConf");
95     if (!clazz) return JNI_ERR;
96
97     g_cls_EnrolleeConf = (jclass)env->NewGlobalRef(clazz);
98     env->DeleteLocalRef(clazz);
99
100     g_mid_EnrolleeConf_ctor = env->GetMethodID(g_cls_EnrolleeConf, "<init>",
101                                 "(Lorg/iotivity/base/OcRepresentation;)V");
102     if (!g_mid_EnrolleeConf_ctor) return JNI_ERR;
103
104     // getEnrolleeStatus
105     clazz = env->FindClass("org/iotivity/service/easysetup/mediator/GetEnrolleeStatus");
106     if (!clazz) return JNI_ERR;
107
108     g_cls_getEnrolleeStatus = (jclass)env->NewGlobalRef(clazz);
109     env->DeleteLocalRef(clazz);
110
111     g_mid_getEnrolleeStatus_ctor = env->GetMethodID(g_cls_getEnrolleeStatus, "<init>",
112                                 "(ILorg/iotivity/service/easysetup/mediator/EnrolleeStatus;)V");
113     if (!g_mid_getEnrolleeStatus_ctor) return JNI_ERR;
114
115     // getConfigurationStatus
116     clazz = env->FindClass("org/iotivity/service/easysetup/mediator/GetConfigurationStatus");
117     if (!clazz) return JNI_ERR;
118
119     g_cls_getConfigurationStatus = (jclass)env->NewGlobalRef(clazz);
120     env->DeleteLocalRef(clazz);
121
122     g_mid_getConfigurationStatus_ctor = env->GetMethodID(g_cls_getConfigurationStatus, "<init>",
123                                 "(ILorg/iotivity/service/easysetup/mediator/EnrolleeConf;)V");
124     if (!g_mid_getConfigurationStatus_ctor) return JNI_ERR;
125
126     // SecurityProvisioningStatus
127     clazz = env->FindClass("org/iotivity/service/easysetup/mediator/SecurityProvisioningStatus");
128     if (!clazz) return JNI_ERR;
129
130     g_cls_SecurityProvisioningStatus = (jclass)env->NewGlobalRef(clazz);
131     env->DeleteLocalRef(clazz);
132
133     g_mid_SecurityProvisioningStatus_ctor = env->GetMethodID(g_cls_SecurityProvisioningStatus, "<init>",
134                                 "(ILjava/lang/String;)V");
135     if (!g_mid_SecurityProvisioningStatus_ctor) return JNI_ERR;
136
137     // DevicePropProvisioningStatus
138     clazz = env->FindClass("org/iotivity/service/easysetup/mediator/DevicePropProvisioningStatus");
139     if (!clazz) return JNI_ERR;
140
141     g_cls_DevicePropProvisioningStatus = (jclass)env->NewGlobalRef(clazz);
142     env->DeleteLocalRef(clazz);
143
144     g_mid_DevicePropProvisioningStatus_ctor = env->GetMethodID(g_cls_DevicePropProvisioningStatus, "<init>",
145                                 "(I)V");
146     if (!g_mid_DevicePropProvisioningStatus_ctor) return JNI_ERR;
147
148     // CloudPropProvisioningStatus
149     clazz = env->FindClass("org/iotivity/service/easysetup/mediator/CloudPropProvisioningStatus");
150     if (!clazz) return JNI_ERR;
151     g_cls_CloudPropProvisioningStatus = (jclass)env->NewGlobalRef(clazz);
152     env->DeleteLocalRef(clazz);
153
154     g_mid_CloudPropProvisioningStatus_ctor = env->GetMethodID(g_cls_CloudPropProvisioningStatus, "<init>",
155                                 "(I)V");
156     if (!g_mid_CloudPropProvisioningStatus_ctor) return JNI_ERR;
157
158     // Integer
159     clazz = env->FindClass("java/lang/Integer");
160     if (!clazz) return JNI_ERR;
161     g_cls_Integer = (jclass)env->NewGlobalRef(clazz);
162     env->DeleteLocalRef(clazz);
163
164     g_mid_Integer_ctor = env->GetMethodID(g_cls_Integer, "<init>",
165                                 "(I)V");
166     if (!g_mid_Integer_ctor) return JNI_ERR;
167
168     //OcRepresentation
169     clazz = env->FindClass("org/iotivity/base/OcRepresentation");
170     if (!clazz) return JNI_ERR;
171     g_cls_OcRepresentation = (jclass)env->NewGlobalRef(clazz);
172     env->DeleteLocalRef(clazz);
173
174     g_mid_OcRepresentation_N_ctor_bool = env->GetMethodID(g_cls_OcRepresentation, "<init>", "(JZ)V");
175     if (!g_mid_OcRepresentation_N_ctor_bool) return JNI_ERR;
176
177     return JNI_CURRENT_VERSION;
178 }
179
180 //JNI OnUnload
181 JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
182 {
183     ES_LOGI("JNI_OnUnload");
184     JNIEnv *env;
185
186     if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
187     {
188         ES_LOGE("Failed to get the environment using GetEnv()");
189         return;
190     }
191     env->DeleteGlobalRef(g_cls_RemoteEnrollee);
192     env->DeleteGlobalRef(g_cls_ESException);
193 }