Merge remote-tracking branch 'origin/extended-easysetup'
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / EasySetupCore / src / main / jni / JniRemoteEnrollee.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
21 #include "JniRemoteEnrollee.h"
22
23 #include "JniOcRepresentation.h"
24
25 using namespace OIC::Service;
26
27 JniRemoteEnrollee::JniRemoteEnrollee(std::shared_ptr<RemoteEnrollee> remoteEnrollee)
28     : m_sharedResource(remoteEnrollee) {}
29
30 JniRemoteEnrollee::~JniRemoteEnrollee()
31 {
32     ES_LOGD("JniRemoteEnrollee::~JniRemoteEnrollee()");
33     m_sharedResource = NULL;
34
35     jint envRet;
36     JNIEnv *env = GetESJNIEnv(envRet);
37     if (NULL == env) return;
38
39     if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
40 }
41
42 JniRemoteEnrollee *JniRemoteEnrollee::getJniRemoteEnrollee(JNIEnv *env, jobject thiz)
43 {
44     JniRemoteEnrollee *remoteEnrollee = ESGetHandle<JniRemoteEnrollee>(env, thiz);
45     if (env->ExceptionCheck())
46     {
47         ES_LOGE("getJniRemoteEnrollee :: Failed to get native handle from RemoteEnrollee object");
48     }
49     if (!remoteEnrollee)
50     {
51         ES_LOGE("getJniRemoteEnrollee :: no resource");
52     }
53     return remoteEnrollee;
54 }
55
56 void JniRemoteEnrollee::getStatus(JNIEnv *env, jobject jListener)
57 {
58     JniGetEnrolleeStatusListener *onGetEnrolleeStatusReceived =
59                     addStatusListener<JniGetEnrolleeStatusListener>(env, jListener);
60
61     GetStatusCb getEnrolleeStatusCallback = [onGetEnrolleeStatusReceived]
62             (std::shared_ptr<OIC::Service::GetEnrolleeStatus > getEnrolleeStatus)
63     {
64         onGetEnrolleeStatusReceived->getEnrolleeStatusCallback(getEnrolleeStatus);
65     };
66
67     try
68     {
69         m_sharedResource->getStatus(getEnrolleeStatusCallback);
70     }
71     catch (ESBadRequestException exception)
72     {
73         ES_LOGE("JNI getStatus :: Exception occured");
74         //throw the exception to java
75         throwESException( env,  exception.what());
76     }
77 }
78
79 void JniRemoteEnrollee::getConfiguration(JNIEnv *env, jobject jListener)
80 {
81     JniGetConfigurationStatusListener *onGetConfigurationStatusReceived =
82                     addStatusListener<JniGetConfigurationStatusListener>(env, jListener);
83
84     GetConfigurationStatusCb getConfigurationStatusCallback = [onGetConfigurationStatusReceived]
85             (std::shared_ptr<OIC::Service::GetConfigurationStatus > getConfigurationStatus)
86     {
87         onGetConfigurationStatusReceived->getConfigurationStatusCallback(getConfigurationStatus);
88     };
89
90     try
91     {
92         m_sharedResource->getConfiguration(getConfigurationStatusCallback);
93     }
94     catch (ESBadRequestException exception)
95     {
96         ES_LOGE("JNI getConfiguration :: Exception occured");
97         //throw the exception to java
98         throwESException( env,  exception.what());
99     }
100 }
101
102 void JniRemoteEnrollee::provisionSecurity(JNIEnv *env, jobject jListener)
103 {
104     JniSecurityStatusListener *onSecurityProvStatusReceived =
105                     addStatusListener<JniSecurityStatusListener>(env, jListener);
106
107     SecurityProvStatusCb secProvStatusCallback = [onSecurityProvStatusReceived]
108             (std::shared_ptr<OIC::Service::SecProvisioningStatus > SecProvisioningStatus)
109     {
110         onSecurityProvStatusReceived->secProvisionStatusCallback(SecProvisioningStatus);
111     };
112
113     try
114     {
115         m_sharedResource->provisionSecurity(secProvStatusCallback);
116     }
117     catch (ESBadRequestException exception)
118     {
119         ES_LOGE("JNI provisionSecurity :: Exception occured");
120         //throw the exception to java
121         throwESException( env,  exception.what());
122     }
123 }
124
125 void JniRemoteEnrollee::provisionDeviceProperties(JNIEnv *env,
126                                                     jobject jRepresentation,
127                                                     jobject jListener)
128 {
129     OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
130     if (!representation)
131     {
132         return;
133     }
134
135     DeviceProp deviceProp(*representation);
136     JniDevicePropProvisioningStatusListener *onDevicePropProvStatusReceived =
137                     addStatusListener<JniDevicePropProvisioningStatusListener>(env, jListener);
138
139     DevicePropProvStatusCb devicePropProvStatusCallback = [onDevicePropProvStatusReceived]
140             (std::shared_ptr<OIC::Service::DevicePropProvisioningStatus > devicePropProvisioningStatus)
141     {
142         onDevicePropProvStatusReceived->onDevicePropProvisioningStatusCallback(devicePropProvisioningStatus);
143     };
144
145     try
146     {
147         m_sharedResource->provisionDeviceProperties(deviceProp, devicePropProvStatusCallback);
148     }
149     catch (ESBadRequestException exception)
150     {
151         ES_LOGE("JNI provisionDeviceProperties :: Exception occured");
152         //throw the exception to java
153         throwESException( env,  exception.what());
154     }
155 }
156
157 void JniRemoteEnrollee::provisionCloudProperties(JNIEnv *env,
158                                                 jobject jRepresentation,
159                                                 jstring jCloudID,
160                                                 jobject jListener)
161 {
162     OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
163     if (!representation)
164     {
165         return;
166     }
167
168     CloudProp cloudProp(*representation);
169     cloudProp.setCloudID(env->GetStringUTFChars(jCloudID, NULL));
170
171     JniCloudPropProvisioningStatusListener *onCloudPropProvisioningStatusReceived =
172                     addStatusListener<JniCloudPropProvisioningStatusListener>(env, jListener);
173
174     CloudPropProvStatusCb cloudPropProvStatusCallback = [onCloudPropProvisioningStatusReceived]
175             (std::shared_ptr< OIC::Service::CloudPropProvisioningStatus > cloudPropProvisioningStatus)
176
177     {
178         onCloudPropProvisioningStatusReceived->onCloudPropProvisioningStatus(cloudPropProvisioningStatus);
179     };
180
181     try
182     {
183         m_sharedResource->provisionCloudProperties(cloudProp, cloudPropProvStatusCallback);
184     }
185     catch (ESBadRequestException exception)
186     {
187         ES_LOGE("JNI startProvisioning :: Exception occured");
188         //throw the exception to java
189         throwESException(env, exception.what());
190     }
191 }
192
193 //JNI
194 JNIEXPORT void JNICALL
195 Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeGetStatus
196 (JNIEnv *env, jobject jClass, jobject jListener)
197 {
198     ES_LOGD("nativeGetStatus Enter");
199
200     JniRemoteEnrollee *remoteEnrollee = JniRemoteEnrollee::getJniRemoteEnrollee(env, jClass);
201     remoteEnrollee->getStatus(env, jListener);
202
203     ES_LOGD("nativeGetStatus Exit");
204 }
205
206 //JNI
207 JNIEXPORT void JNICALL
208 Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeGetConfiguration
209 (JNIEnv *env, jobject jClass, jobject jListener)
210 {
211     ES_LOGD("nativegetConfiguration Enter");
212
213     JniRemoteEnrollee *remoteEnrollee = JniRemoteEnrollee::getJniRemoteEnrollee(env, jClass);
214     remoteEnrollee->getConfiguration(env, jListener);
215
216     ES_LOGD("nativegetConfiguration Exit");
217 }
218
219 JNIEXPORT void JNICALL
220 Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeProvisionSecurity
221 (JNIEnv *env, jobject jClass, jobject jListener)
222 {
223     ES_LOGD("nativeStartSecurityProvision Enter");
224
225     JniRemoteEnrollee *remoteEnrollee = JniRemoteEnrollee::getJniRemoteEnrollee(env, jClass);
226     remoteEnrollee->provisionSecurity(env, jListener);
227
228     ES_LOGD("nativeStartSecurityProvision Exit");
229 }
230
231 JNIEXPORT void JNICALL
232 Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeProvisionDeviceProperties
233 (JNIEnv *env, jobject jClass, jobject jRepresentation, jobject jListener)
234 {
235     ES_LOGD("nativeProvisionDeviceProperties Enter");
236
237     JniRemoteEnrollee *remoteEnrollee = JniRemoteEnrollee::getJniRemoteEnrollee(env, jClass);
238     remoteEnrollee->provisionDeviceProperties(env, jRepresentation, jListener);
239
240     ES_LOGD("nativeProvisionDeviceProperties Exit");
241 }
242
243 JNIEXPORT void JNICALL
244 Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeProvisionCloudProperties
245 (JNIEnv *env, jobject jClass, jobject jRepresentation, jstring jCloudID, jobject jListener)
246 {
247     ES_LOGD("nativeprovisionCloudProperties Enter");
248
249     JniRemoteEnrollee *remoteEnrollee = JniRemoteEnrollee::getJniRemoteEnrollee(env, jClass);
250     remoteEnrollee->provisionCloudProperties(env, jRepresentation, jCloudID, jListener);
251
252     ES_LOGD("nativeprovisionCloudProperties Exit");
253 }