Imported Upstream version 1.2.0
[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                                                 jint jCredID,
161                                                 jobject jListener)
162 {
163     OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
164     if (!representation)
165     {
166         return;
167     }
168     if (!jCloudID)
169     {
170         return;
171     }
172
173     CloudProp cloudProp(*representation);
174     cloudProp.setCloudID(env->GetStringUTFChars(jCloudID, NULL));
175     cloudProp.setCredID(jCredID);
176
177     JniCloudPropProvisioningStatusListener *onCloudPropProvisioningStatusReceived =
178                     addStatusListener<JniCloudPropProvisioningStatusListener>(env, jListener);
179
180     CloudPropProvStatusCb cloudPropProvStatusCallback = [onCloudPropProvisioningStatusReceived]
181             (std::shared_ptr< OIC::Service::CloudPropProvisioningStatus > cloudPropProvisioningStatus)
182
183     {
184         onCloudPropProvisioningStatusReceived->onCloudPropProvisioningStatus(cloudPropProvisioningStatus);
185     };
186
187     try
188     {
189         m_sharedResource->provisionCloudProperties(cloudProp, cloudPropProvStatusCallback);
190     }
191     catch (ESBadRequestException exception)
192     {
193         ES_LOGE("JNI startProvisioning :: Exception occured");
194         //throw the exception to java
195         throwESException(env, exception.what());
196     }
197 }
198
199 //JNI
200 JNIEXPORT void JNICALL
201 Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeGetStatus
202 (JNIEnv *env, jobject jClass, jobject jListener)
203 {
204     ES_LOGD("nativeGetStatus Enter");
205
206     JniRemoteEnrollee *remoteEnrollee = JniRemoteEnrollee::getJniRemoteEnrollee(env, jClass);
207     remoteEnrollee->getStatus(env, jListener);
208
209     ES_LOGD("nativeGetStatus Exit");
210 }
211
212 //JNI
213 JNIEXPORT void JNICALL
214 Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeGetConfiguration
215 (JNIEnv *env, jobject jClass, jobject jListener)
216 {
217     ES_LOGD("nativegetConfiguration Enter");
218
219     JniRemoteEnrollee *remoteEnrollee = JniRemoteEnrollee::getJniRemoteEnrollee(env, jClass);
220     remoteEnrollee->getConfiguration(env, jListener);
221
222     ES_LOGD("nativegetConfiguration Exit");
223 }
224
225 JNIEXPORT void JNICALL
226 Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeProvisionSecurity
227 (JNIEnv *env, jobject jClass, jobject jListener)
228 {
229     ES_LOGD("nativeStartSecurityProvision Enter");
230
231     JniRemoteEnrollee *remoteEnrollee = JniRemoteEnrollee::getJniRemoteEnrollee(env, jClass);
232     remoteEnrollee->provisionSecurity(env, jListener);
233
234     ES_LOGD("nativeStartSecurityProvision Exit");
235 }
236
237 JNIEXPORT void JNICALL
238 Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeProvisionDeviceProperties
239 (JNIEnv *env, jobject jClass, jobject jRepresentation, jobject jListener)
240 {
241     ES_LOGD("nativeProvisionDeviceProperties Enter");
242
243     JniRemoteEnrollee *remoteEnrollee = JniRemoteEnrollee::getJniRemoteEnrollee(env, jClass);
244     remoteEnrollee->provisionDeviceProperties(env, jRepresentation, jListener);
245
246     ES_LOGD("nativeProvisionDeviceProperties Exit");
247 }
248
249 JNIEXPORT void JNICALL
250 Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeProvisionCloudProperties
251 (JNIEnv *env, jobject jClass, jobject jRepresentation, jstring jCloudID, jint jCredID, jobject jListener)
252 {
253     ES_LOGD("nativeprovisionCloudProperties Enter");
254
255     JniRemoteEnrollee *remoteEnrollee = JniRemoteEnrollee::getJniRemoteEnrollee(env, jClass);
256     remoteEnrollee->provisionCloudProperties(env, jRepresentation, jCloudID, jCredID, jListener);
257
258     ES_LOGD("nativeprovisionCloudProperties Exit");
259 }